Walking animation cycling wonky, knockback causes retreat

After adding the impact state, two things started happening. For one, after the enemy gets knocked back, sometimes they start backing away instead, until they are past detection range. It first, I thought I’d fixed it by adding another statement to ensure the enemy was facing the player, but that only fixed the situation where they’d lost sight of me. Sometimes it’s backwards, and it stops when they exit detection range. Sometimes, it’s more like they’re running Perpendicular, and get stuck, running in place.

The second issue, but which also showed up around the time I added the impact state (as far as I can tell) is that when I hold down a movement key in one direction, the first cycle or so of the animation looks like, but then it starts restarting the animation over and over again, just sort of jittering in the first part of the animation.

EnemyChaseState:

public override void Tick(float deltaTime)
{
    MoveToPlayer(deltaTime);

    FacePlayer();
    
    if (!IsInChaseRange())
    {
        stateMachine.SwitchState(new EnemyIdleState(stateMachine));
        return;
    } else if (IsInAttackRange())
    {
        stateMachine.SwitchState(new EnemyAttackingState(stateMachine));
        return;
    }

    stateMachine.Animator.SetFloat(SpeedHash, 1f, AnimatorDampTime, deltaTime);
}

public void MoveToPlayer(float deltaTime)
{
    if (stateMachine.Agent.isOnNavMesh)
    {
        Vector3 destination = this.stateMachine.Player.transform.position;
        stateMachine.Agent.destination = destination;

        Move(stateMachine.Agent.desiredVelocity.normalized * this.stateMachine.MovementSpeed, deltaTime);

        // Debug.Log(string.Format("Destination: {0}, direction: {1}, Speed: {2}", destination, stateMachine.Agent.desiredVelocity.normalized, this.stateMachine.MovementSpeed));

        FacePlayer();
    }

    stateMachine.Agent.velocity = stateMachine.Controller.velocity;
}

PlayerFreeLookState:

public override void Enter() {
    stateMachine.InputReader.JumpEvent += OnJump;
    stateMachine.InputReader.DodgeEvent += OnDodge;
    stateMachine.InputReader.TargetEvent += OnTarget;

    stateMachine.Animator.CrossFadeInFixedTime(FreeLookBlendTreeHash, FixedTransitionDuration);
}

public override void Tick(float deltaTime)
{
    if (stateMachine.InputReader.IsAttacking)
    {
        stateMachine.SwitchState(new PlayerAttackingState(stateMachine, 0));
        return;
    }

    Vector3 movement = CalculateMovement();

    Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);

    if (stateMachine.InputReader.MovementValue == Vector2.zero) {
        stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0f, AnimatorDampTime, deltaTime);
        return;
    }

    stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1.0f, AnimatorDampTime, deltaTime);

    FaceMovementDirection(movement, deltaTime);
}

public override void Exit() {
    stateMachine.InputReader.DodgeEvent -= OnDodge;
    stateMachine.InputReader.JumpEvent -= OnJump;
    stateMachine.InputReader.TargetEvent -= OnTarget;
}

Can you show the impact state?

Everything else looks ‘ok’. You don’t need FacePlayer() in the enemy’s MoveToPlayer() method because you are going to call it immediately after that again anyway.

Also, there’s an issue that happens with the enemy’s NavMeshAgent. The agent’s position goes out of sync with the CharacterController. It’s not fixed in the course but there is enough posts here that addresses the issue. In your enemy’s MoveToPlayer() method, change the last line to this

    //stateMachine.Agent.velocity = stateMachine.Controller.velocity;
    stateMachine.Agent.nextPosition = stateMachine.transform.position;

You can see the issue if you play for a little while and then look in the scene view with the enemy selected. The NavMeshAgent would not be quite where the enemy is (obviously the enemy would have had to move around a little)

1 Like

Thank you. That fixed the issue with enemy navigation and, weirdly enough, also seems to have fixed player motion. No idea why it would have done the latter…

Privacy & Terms