Enemy running on spot after attacking


I’ve got an issue where the enemy can’t move after the player is knocked back. He just play his chasing animation but the velocity is 0.
He only moves again to get in range if the player moves.

i think my enemy chasing state script is pretty much the same as the one in the lecture. I can’t figure out the origin of the bug when the navmesh destination seems to be correct.

public class EnemyChasingState : EnemyBaseState
{

private readonly int LocomotionBlendTreeHash = Animator.StringToHash("LocomotionBlendTree");


private readonly int SpeedHash = Animator.StringToHash("Speed");

private const float AnimatorDampTime = 0.1f;

private const float CrossFadeDuration = 0.1f;


public EnemyChasingState(EnemyStateMachine stateMachine) : base(stateMachine) { }

public override void Enter()
{
    stateMachine.Animator.CrossFadeInFixedTime(LocomotionBlendTreeHash, CrossFadeDuration);
}
public override void Tick(float deltaTime)
{
    if (!IsInChaseRange())
    {
        stateMachine.SwitchState(new EnemyIdleState(stateMachine));
        return;
    }
    else if (IsInAttackRange()) 
    {
        stateMachine.SwitchState(new EnemyAttackingState(stateMachine));
    }
    MoveToPlayer(deltaTime);
    FacePlayer();

    stateMachine.Animator.SetFloat(SpeedHash, 1f, AnimatorDampTime, deltaTime);
}
public override void Exit() 
{
    stateMachine.Agent.ResetPath();
    stateMachine.Agent.velocity= Vector3.zero;
}

private void MoveToPlayer(float deltaTime)
{
    if(stateMachine.Agent.isOnNavMesh)
    {
        stateMachine.Agent.destination = stateMachine.Player.transform.position;
        Move(stateMachine.Agent.desiredVelocity.normalized * stateMachine.MovementSpeed, deltaTime);
        Debug.Log("Destination" + stateMachine.Agent.destination);
        Debug.Log("player pos" + stateMachine.Player.transform.position);
        Debug.Log("speed" + stateMachine.MovementSpeed);
    }
    stateMachine.Agent.velocity = stateMachine.Controller.velocity;
    Debug.Log(" stateMachine.Agent.velocity " + stateMachine.Agent.velocity);
    Debug.Log("stateMachine.Controller.velocity" + stateMachine.Controller.velocity);
}

private bool IsInAttackRange()
{
    float playerDistanceSqr = (stateMachine.Player.transform.position - stateMachine.transform.position).sqrMagnitude;

    return playerDistanceSqr <= stateMachine.AttackRange * stateMachine.AttackRange;
   
}

}

Check to make sure that your Targeter’s collider is set to Is Trigger. The last time I saw this particular bug, the issue was that it wasn’t set to Trigger. If the character moves within range to attack the enemy, the CapsuleCollider on the enemy doesn’t get blocked by the collider because he’s already within it, but the knockback pushes him out of the collider’s range so when the enemy tries to come back, then the Capsule collider detects the collission and prevents the enemy from getting closer.

Thank you for your reply. yhe collider was on trigger. i took a new capture with the colliders visible.

i’ve made another test. the problem comes from the navmesh position not stopping at the enemy position.

As a cheap fix, if i disable the navmesh agent in the Exit() method and enable it in the Enter() method, it updates the actual position of the enemy for the navmesh.
There’s probably a better solution but it’s what i’ve found so far.

I think I see what’s happening (and for the first time figured out why).
In our MoveToPlayer() code, we’re adjusting the NavMeshAgent’s velocity correctly

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

The trouble arises when the ForceReceiver moves the character, the NavMeshAgent doesn’t take this into account correctly.
Try adding this line to the end of the MoveToPlayer() method:

stateMachine.Agent.nextPosition = stateMachine.transform.position;

Thank you, it works perfectly with that line!

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

Privacy & Terms