A very strange bug when in the falling state

this may be a bit long winded because theres alot to this bug…

when i enter play mode everything starts just fine the locomotion blend tree is operating like normal and when i jump it enters the jump state and when im falling it enters the fall state heres where it gets weird

when im done falling it both does and doesn’t go back into the locomotion blend tree the animator still shows my player as being in the fall state however all of the logic from my “FreeLookState” is still working fine and the float for the blend tree is still updating my player is even still turning to face its movement direction however the blend tree animations are not playing

even weirder is while its in play mode if i move the fall state in the Animator window it snaps my player back into the blend tree and starts working like normal again until i jump again then it repeats this entire process i cannot figure out if this is a bug in my code or a bug in Unity but either way i have no idea how to fix it

heres my fall state

public class PlayerFallState : PlayerBaseState
{
    readonly int fallHash = Animator.StringToHash("Fall");

    const float crossFadeDuration = 0.1f;

    Vector3 momentum;

    public PlayerFallState(PlayerStateMachine stateMachine) : base(stateMachine)
    {
    }

    public override void Enter()
    {
        momentum = stateMachine.Controller.velocity;
        momentum.y = 0;

        stateMachine.Animator.CrossFadeInFixedTime(fallHash, crossFadeDuration);
    }

    public override void Tick(float deltaTime)
    {
        Move(momentum, deltaTime);

        if (stateMachine.Controller.isGrounded)
        {
            ReturnToLocomotion();
        }
    }

    public override void Exit()
    {

    }
}

heres my “FreeLookState”

public class PlayerMoveState : PlayerBaseState
{
    readonly int forwardSpeedHash = Animator.StringToHash("forwardSpeed");

    const float animatorDampTime = 0.1f;

    public PlayerMoveState(PlayerStateMachine stateMachine) : base(stateMachine) { }

    public override void Enter()
    {
        stateMachine.InputReader.JumpAction += OnJump;
    }

    public override void Tick(float deltaTime)
    {
        Vector3 movement = CalculateMovement();

        Move(movement * stateMachine.PlayerSpeed, deltaTime);

        if (stateMachine.InputReader.MovementValue == Vector2.zero)
        {
            stateMachine.Animator.SetFloat(forwardSpeedHash, 0, animatorDampTime, deltaTime);

            return;
        }

        stateMachine.Animator.SetFloat(forwardSpeedHash, 1, animatorDampTime, deltaTime);
        FaceMoveDirection(movement, deltaTime);
    }

    public override void Exit()
    {
        stateMachine.InputReader.JumpAction -= OnJump;
    }

    void OnJump()
    {
        stateMachine.SwitchState(new PlayerJumpState(stateMachine));
    }

    Vector3 CalculateMovement() // Calculates camera relative movement
    {
        Vector3 cameraForward = stateMachine.MainCameraTransform.forward;
        cameraForward.y = 0;
        cameraForward.Normalize();

        Vector3 cameraRight = stateMachine.MainCameraTransform.right;
        cameraRight.y = 0;
        cameraRight.Normalize();

        return cameraForward * stateMachine.InputReader.MovementValue.y + cameraRight * stateMachine.InputReader.MovementValue.x;
    }

    void FaceMoveDirection(Vector3 movement, float deltaTime)
    {
        stateMachine.transform.rotation = Quaternion.Lerp(
            stateMachine.transform.rotation,
            Quaternion.LookRotation(movement),
            deltaTime * stateMachine.PlayerRotationDamping);
    }

now my game does not have combat or enemies so the code mostly lacks logic involved in those lectures

nevermind i found the 2 lines of code i apparently missed so this issue has been solved

In your FreelookState, you are not switching the AnimatorState into the FreeLookState with

stateMachine.Animator.CrossFadeInFixedTime()

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms