Stopping Our Agents
General AI Behaviours
- Go to the where the player is
- Attacking the Player
- Stopping Actions Upon Death
- 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.
IsPlayerAlive
and Victory
is set to false.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!
Now you should get a behaviour like the following, where the agents stop attacking when the player dies.