Stopping Our Agents

General AI Behaviours

  1. Go to the where the player is
  2. Attacking the Player
  3. Stopping Actions Upon Death
  4. Stopping the Agent on Death

3. Stopping Actions Upon Player Death

When the player dies, the agent continuously attacks the player, so you’ll see a constant flashing red screen. This unwanted behaviour comes from the idea that we don’t have another context/decision that takes precedence over Attack Player.

We’ve already created an Observer, IsPlayerAlive. We can use that very same Observer and hook it up to another Decision. Create a new Decision, Victory, in the editor. Since Victory should have a higher Priority than Attack Player make the Priority 2, and attach it to the IsPlayerAlive Observer.

victory-conditions

Stopping

Finally, we need to stop our agent from movement. A simple Action script, Stop.cs is all the agent needs.

using UnityEngine;
using UnityEngine.AI;

public class Stop : Action {

    private NavMeshAgent navAgent;

    public override void OnStart () {
        // Get the NavMeshAgent component
        navAgent = GetComponent<NavMeshAgent> ();
    }

    public override void OnActionStart () {
        // Clear the path and disable the NavMeshAgent
        if (navAgent.isOnNavMesh) {
            navAgent.isStopped = true;
            navAgent.ResetPath();
        }
        navAgent.enabled = false;
    }

    public override ActionState OnActionUpdate () {
        // Immediately succeed the task immediately
        return ActionState.Success;
    }
}

Once compiled, add the Action to the editor!

player-death-stop

Now you should get a behaviour like the following, where the agents stop attacking when the player dies.

player-death