- Home /
Stop player rotation when there is no movement
I want to stop my character from rotating with the camera when the character is still. I am not sure what the best way to go about it is. In my character controller I am using the new unity input system and for camera I am using Cinemachine, on my character I have the character controller component.
Here is the rotation code:
// Rotate towards camera direction
float targetAngle = cameraTransform.eulerAngles.y;
Quaternion targetRotation = Quaternion.Euler(0, targetAngle, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
and here is the movement code:
Vector2 input = moveAction.ReadValue<Vector2>();
Vector3 move = new Vector3(input.x, 0, input.y);
move = move.x * cameraTransform.right.normalized + move.z * cameraTransform.forward.normalized;
move.y = 0f;
controller.Move(move * Time.deltaTime * playerSpeed);
Comment
Best Answer
Answer by tyruji · Jul 22, 2021 at 12:12 PM
You could make a Vector3 field _lastInput or whatever and store your input in the movement function.
then in your rotation function just add
if( _lastInput.sqrMagnitude == 0 ) return;
at the beginning. That should do it.
How would I store the input in the movement with that field?
your movement code would look like this:
Vector2 input = moveAction.ReadValue<Vector2>();
_lastInput = input;
// etc...