Player Model looking direction after movement

I am creating a 3D-third Person Project with a Free-Look camera.


The player-model should face in the direction moved (eg. player pressing “D” → player-model should look to the right side even after button is released ).

I have implemented the logic that will turn the player-model into the right direction. However after releasing the movement button the player-model will always turn back facing the initial forward direction.
So the problem I am facing is, that the looking direction isn’t preseved.


public class CharMovement : MonoBehaviour
{
    // Variables
    private float movementSpeed = 5.0f;
    private Vector3 velocityVector3;
    private Vector3 movementVector;
    private Vector2 movementInput;

    //Setting Variables
    [SerializeField] private float RotatingSpeed = 500.0f;
    [SerializeField] private bool isGrounded;
    [SerializeField] private float groundCheckDistance = 0.2f;
    [SerializeField] private LayerMask groundLayerMask;
    [SerializeField] private float gravity = -9.81f;
    [SerializeField] private float jumpHeight = 1.0f;

    // KeyCodes
    [SerializeField] private KeyCode jumpKCode = KeyCode.Space;

    // Objects - References
    private CharacterController characterController;
    private Animator animator;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        animator = GetComponentInChildren<Animator>();
        animator.applyRootMotion = false; 
    }

    void Update()
    {
        movementInput = GetInput();
        Move();
    }
	
	private Vector2 GetInput()
    {
        float dX = Input.GetAxisRaw("Horizontal") * movementSpeed; // A-D
        float dZ = Input.GetAxisRaw("Vertical") * movementSpeed; // W-S
       return new Vector2(dX, dZ); 
    }

    private void Move()
    {
        isGrounded =  //...
        
        movementVector = new Vector3(movementInput.x * Time.deltaTime, 0, movementInput.y * Time.deltaTime);
        if (isGrounded)
        {
            if (movementVector != Vector3.zero) {Run();}
            else {Idle();}
			
            // normalize & transform to global coordinate System
            movementVector = Vector3.ClampMagnitude(movementVector, movementSpeed); 
            movementVector = transform.TransformDirection(movementVector);             

            // Rotate character to direction
            if (! Input.GetKeyDown(KeyCode.S))
            {
                Quaternion toRotation = Quaternion.LookRotation(movementVector, Vector3.up);
                transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, RotatingSpeed * Time.deltaTime);
            }

            if (Input.GetKeyDown(jumpKCode)){Jump();}
        }

        // transform.Translate(movementVector);
        characterController.Move(movementVector);

        // calculate &apply gravity to player
        velocityVector3.y += gravity * Time.deltaTime;
        characterController.Move(velocityVector3 * Time.deltaTime);
    }

    private void Idle(){ animations }
    private void Run(){ // animations }

    private void Jump()
    {velocityVector3.y = Mathf.Sqrt(jumpHeight * -2 * gravity); .... }
}

[sorry for the bad formatting]

You can use this code and add gravity according to you

private void Update()
    {
        input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        inputDir = input.normalized;
        if (input != Vector2.zero)
        {
            rotationAngle = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
            targetRotation = new Vector3(0, rotationAngle, 0);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(targetRotation), smoothRotSpeed * Time.deltaTime);
            controller.Move(transform.forward * moveSpeed * Time.deltaTime);
        }
    }