Hi guys, i have a problem with the rotation of the player in 3D. First of all this is my code:
private void OnEnable()
{
m_playerActions.PlayerControls.Jump.performed += PlayerJump;
m_playerActions.PlayerControls.Jump.canceled -= PlayerJump;
m_playerActions.PlayerControls.Movement.performed += ctx => move = ctx.ReadValue<Vector2>();
m_playerActions.PlayerControls.Movement.canceled += ctx => move = Vector2.zero;
m_playerActions.PlayerControls.Enable();
}
private void OnDisable()
{
m_playerActions.PlayerControls.Disable();
}
private void PlayerJump(InputAction.CallbackContext ctx)
{
if(m_isGrounded)
{
Debug.Log("Jump");
m_rb.AddForce(Vector3.up * m_jumpForce, ForceMode.Impulse);
}
}
private void FixedUpdate()
{
Vector3 moveDir = new Vector3(move.x, 0.0f, move.y);
moveDir = cam.transform.TransformDirection(moveDir);
moveDir.y = 0.0f;
moveDir = moveDir.normalized * m_playerSpeed * Time.fixedDeltaTime;
m_rb.MovePosition(transform.position + moveDir);
Quaternion targetRotation = Quaternion.LookRotation(moveDir);
targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 * Time.fixedDeltaTime);
m_rb.MoveRotation(targetRotation);
}
What i want to achieve is basically a script movement in third person with a free camera (i’m using the cinemachine) and with the player that always rotates and goes in camera direction. Everything works fine, but my only problem is that when i stop pressing the movements buttons, the rotation of my player always goes back to zero. I’m using the new Unity Input System and i think the issue may be caused by the fact that the values of the movement return to zero when the action is cancelled, this is the line:
m_playerActions.PlayerControls.Movement.canceled += ctx => move = Vector2.zero;
I just want my player to keep facing the direction he was following after he stops.
However i don’t know how to solve this, do you have any solution?
Thanks for your time
