Make the player face his movement direction script problem

Hello,

Searching a way to face the movement direction of the player, I found this script made by PaxForce:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseRotate : MonoBehaviour {

    public float movementSpeed = 10f;

    
 void Update () {
     
         ControllPlayer();
     
     
     }
 
 
     void ControllPlayer()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
 float moveVertical = Input.GetAxis ("Vertical");
  
 Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
         
          if (movement != Vector3.zero) {
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized), 0.2f);
     }
 
 transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);
         
     }
 }

However everytime the player moves it works perfectly, but it goes to the default rotation after the controls are released.

Can anyone help me?

Thank you!

you are rotating the player even when the input axis are 0 so the player will start rotating towards vector3 zero.

void ControllPlayer()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

if(moveHorizontal == 0 && moveVertical == 0) return;

Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);

       if (movement != Vector3.zero) {
      transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized), 0.2f);
  }

transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);

  }