Animation sometimes freezes on first frame

Sometimes when walking, regardless of the direction, the motion animation freezes on the first frame. In this situation, only restarting the scene helps.

It seems to me that it’s either the animator’s settings or the script that controls the animation.

Animator Window, Base Layer


Animator Window, Walking

Animation Controller Script

using Cinemachine;
using Player;
using UnityEngine;

public class AnimationController : MonoBehaviour
{
    #region AccessableFields
    [Header("TransitionSpeed")]
    [SerializeField] private float _towardsTransitionSpeed = 0.2f;
    [SerializeField] private float _backwardsTransitionSpeed = 0.1f;
    [SerializeField] private float _stopTransitionSpeed = 0.1f;

    [Header("Camera")]
    [SerializeField] private CinemachineFreeLook _camera;

    [Header("Animation")]
    [SerializeField] private Animator _animator;

    [Header("ActiveRagdoll")]
    [SerializeField] private ActiveRagdollController _activeRagdollController;
    #endregion

    #region PrivateFields
    private InputSystem _inputSystem;

    private float _mouseXSmooth;
    #endregion

    private void OnEnable()
    {
        _inputSystem = new InputSystem();
        _inputSystem.Player.Enable();
    }

    private void Update()
    {
        if (_inputSystem.Player.S.IsPressed())
        {
            if (_inputSystem.Player.Sprint.IsPressed()) _animator.SetFloat("yAxis", -2, _backwardsTransitionSpeed, Time.deltaTime);
            else _animator.SetFloat("yAxis", -1, _backwardsTransitionSpeed, Time.deltaTime);
        }
        else if (_inputSystem.Player.W.IsPressed() || _inputSystem.Player.A.IsPressed() || _inputSystem.Player.D.IsPressed())
        {
            if (_inputSystem.Player.Sprint.IsPressed()) _animator.SetFloat("yAxis", 2, _towardsTransitionSpeed, Time.deltaTime);
            else _animator.SetFloat("yAxis", 1, _towardsTransitionSpeed, Time.deltaTime);
        }
        else if (_animator.GetFloat("yAxis") != 0)
        {
            _animator.SetFloat("yAxis", 0, _stopTransitionSpeed, Time.deltaTime);
        }

        if (_camera.m_XAxis.m_InputAxisValue != 0 && !_inputSystem.Player.Movement.IsInProgress())
        {
            _animator.SetBool("isTurning", true);

            _mouseXSmooth = Mathf.Lerp(_mouseXSmooth, _inputSystem.Player.Look.ReadValue<Vector2>().x, 4 * Time.deltaTime);
            _animator.SetFloat("Turn", _mouseXSmooth);
        }
        else if (_animator.GetBool("isTurning")) _animator.SetBool("isTurning", false);
    }

    private void OnDisable() => _inputSystem.Player.Disable();
}

I would appreciate any kind of help.

Next time it happens, press pause, find your script above, delete it (just the script), press Pause again to resume playing, select the Animator on the problematic character and start manipulating the properties to see if you can unstick it.

Anything with Animations / Animators / Mechanim:

Only consider the code AFTER you have done this critical step:

Always start with the Animator state machine and prove it works in isolation, no code at all.

Here’s more reading:

Once you are confident the animator is correct, that means you just have written a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

1 Like