Unity3D Обнуляется значение поворота

В игре от третьего лица сделал поворот персонажа в сторону движения относительно камеры с помощью Quaternion.LookRotation и интерполяции. Проблема в том, что после завершения движения, то есть после того как отпускается кнопка, значение поворота обнуляется. Подскажите как решить проблему

public class PlayerHandler : MonoBehaviour
{
    public float speed = 5;
    private CharacterController characterController;
    private Vector3 moveDirection;
    public new Transform camera;

    void Start() => characterController = GetComponent<CharacterController>();

    private void FixedUpdate() {
        var horizontal = Input.GetAxis("Horizontal") * speed;
        var vertical = Input.GetAxis("Vertical") * speed;
        moveDirection = new Vector3(horizontal, 0, vertical);
        moveDirection = Vector3.ClampMagnitude(moveDirection, speed);
        moveDirection *= Time.deltaTime;

        moveDirection = camera.TransformDirection(moveDirection);
        moveDirection.y = -9.8f;

        Quaternion rotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0, moveDirection.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);

        characterController.Move(moveDirection);

    }

}

Ответы (1 шт):

Автор решения: Timalice
if(horizontal!=0 || vertical!=0) {
    Quaternion rotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0, moveDirection.z));
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);
}
→ Ссылка