- Home /
Question by
ealva479 · Aug 27, 2018 at 08:31 PM ·
controllerplayer movementcamera rotate
Make player face same direction as camera
My player controller script has the player move in the direction the camera is facing, but only when I press forward/backward. I'm wanting the player to constantly face the same direction as the camera (left & right only), but can't seem to figure out a way to do it.
This is in my update method:
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero) {
float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
bool running = Input.GetKey (KeyCode.LeftShift);
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp (currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
transform.Translate (transform.forward * currentSpeed * Time.deltaTime, Space.World);
float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat ("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
Comment