Character turns around when moving but doesn´t stay facing the direction it moved when stop moving around

hello, I´m new here and i´m just starting to learn Unity.
I¨m making a script to make my character turns around to the dirrection it is moving but my problem is that the character doesn´t stay facing in the same direction when it stops moving.
When i test it I get the message in the console “Look rotation viewing is zero”. i tried to fix it by adding “0.001f” in the “vector 3 MoveDir” right next to “inputvector.x” but it doesn´t work, the problem is still there. And I´m not really sure how to fix it.

Here is the script that I made:

public class Playercontrolerscript : MonoBehaviour
{
    [SerializeField] private float MoveSpeed = 7f;
    private void Update()
    {   
        Vector2 InputVector = new(0, 0);

        if (Input.GetKey(KeyCode.W))
        {
            InputVector.y = +1;
        }

        if (Input.GetKey(KeyCode.S))
        {
            InputVector.y = -1;
        }

        if (Input.GetKey(KeyCode.A))
        {
            InputVector.x = -1;
        }

        if (Input.GetKey(KeyCode.D))
        {
            InputVector.x = +1;
        }

        InputVector = InputVector.normalized;

        Vector3 MoveDir = new(InputVector.x, 0f, InputVector.y);
        transform.position += MoveSpeed * Time.deltaTime * MoveDir;

        float rotatespeed = 10f;
        transform.forward = Vector3.Slerp(transform.forward, MoveDir, Time.deltaTime * rotatespeed);
    }
}

That’s a message that I associate with Quaternion.LookRotation, not transform.forward. Perhaps Unity Tech changed something in version 2022-3-LTS. It doesn’t return the message in version 2021.3.

A workaround:

    [SerializeField] private float MoveSpeed = 7f;
    void Update()
    {
        Vector3 MoveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
        if (MoveDir.magnitude>0.01f)
        {
            transform.position += MoveDir * MoveSpeed * Time.deltaTime;
            transform.forward = Vector3.Slerp(transform.forward, MoveDir, Time.deltaTime * 10);
        }
    }
2 Likes

Ah, I see. Maybe it is something related to the different versions.
I did use the workaround that you provided me and it did work. I have been dealing with this problem for days, So, thank you a lot for the feedback.
I didn´t use the quartenions since I don´t know how to use them yet. That´s why I went with the “transform.forward”.