Problem with 3D player rotation in third person

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

You are already waaaaay ahead of the game by choosing Cinemachine… congrats!

Now just take the next step and hit the google!

7402322--904649--Screen Shot 2021-08-09 at 8.47.23 AM.jpg

I’ve already seen some different videos and checked different similar questions online, but I haven’t managed to find the solution to my problem yet. This is why I created a thread here in the Unity Forum with my code

Anyway i found the solution, i’ve just added this “If” to my code, it was much easier than i thought

if(moveDir != Vector3.zero) {
Quaternion targetRotation = Quaternion.LookRotation(moveDir);
        targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 *    Time.fixedDeltaTime);
        m_rb.MoveRotation(targetRotation);
}
1 Like

One way to think about camera things is “where is the eyeball” and “what is the eyeball looking at?”

If you break those two things out into separate GameObjects, and tell Cinemachine “Go here, look there,” then you can focus on just moving those two things around to get the effects you want.