Rotation snapping to 0 after movement

After fixing the rotation of my player character, she worked normally until I added an animation controller and she started snapping back to 0 on only the Y axis once the movement stopped. I removed the animation controller and the new code that accompanied it hoping it would fix the problem so I could try again, but even after removing it she still snaps forward.
I’m not sure if this is a problem with the blender model or the script, at this point.
(Sorry again for a potentially simple question ;;;:wink:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    public float speed = 18;

    private Rigidbody rig;

    // Use this for initialization
    void Start ()
    {
        rig = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update ()
    {
        float hAxis = Input.GetAxis("Horizontal");
        float vAxis = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;

        rig.MovePosition(transform.position + movement);
        rig.MoveRotation(Quaternion.LookRotation(movement));
    }
}

That’s because when you stop giving input hAxis and vAxis will get set to 0, which will set movement to be 0,0,0. Since your rotation is based off movement its going to snap to the exact same rotation when you’re movement is 0,0,0 everytime, regardless of what direction your character was previously facing.

Thank you for the quick answer! How would I go about fixing that in the code?

I don’t usually work with user input but a quick solution would be to wrap rig.MoveRotation in an if statement checking the movement is not zero.This way will mean it will keep the rotation from when the previous direction the rig was moving when it has stopped.

if (movement != Vector3.zero)
{
    rig.MoveRotation(Quaternion.LookRotation(movement));
}

There might be a more relevant api call that Unity provides which you can check against though.

Awesome, that did the trick :):slight_smile: Thank you so much!