Chasing a Player

This guide will create an agent behavior that chases a player. If there isn't a player in range the agent will instead wander around aimlessly.

Creating the wandering behavior

  1. Create a new tree and rename it "Wander".

  2. Open the new tree and add a new "Sequence" node.

  3. Connect the "Root" node to the "Sequence" node.

  4. Create a "Wander" and connect it to the "Sequence" node. The radius parameter controls how far the agent will wander.

  5. Create a new "Wait" node and connect it to the "Sequence" node as well. The duration parameter controls how long the agent should wait.

Setting up the scene

  1. Open the scene "Wander" in "Examples/Tutorials". This scene has a baked navmesh and a simple GameObject with a NavMeshAgent component.

  2. Add a BadgerAgent component to the GameObject and in the "Asset" field select the newly created "Wander" tree.

  1. Press play! The agent will now wander around the scene.

Creating the chasing behavior

  1. Create a new tree and name it "Chase".

  2. Create a new blackboard and add a new GameObject key called "Target".

  1. Assign the new blackboard to the "Chase" tree. This enables us to add "Target" as a node condition.

  1. Open the "Chase" tree and add a "MoveToObject" node. The threshold parameter determines how far the target GameObject has to move before we re-calculate a new path. In the GameObject field, we can now select the "Target" key we made previously. Add the "Target" key as a condition and set it to "NotNull". This node will only be explored if our agent has a "Target" in its blackboard.

  2. Add a new "SubTree" node and assign the "Wander" tree we made before. BadgerHTN supports modular/reusable trees and you can view the search state of multiple trees simultaneously.

  3. Connect both of the new nodes to the "Root" node. The "Root" node behaves in the same way as a "Selector" node and will first explore the "MoveToObject" node and then the "SubTree".

Ad

Adding a player to the scene

Our new agent needs something to target so let's add a player to the scene. Add a new Player GameObject and add a player controller to it. You can add any type of controller you want and there is a sample controller in the "Examples/Guides/02 Chase" scene.

Making a targeting script

Our new chase behavior requires that the agent has a target before starting the chase. We can make a simple script that updates the agent's blackboard with target information. Create a new MonoBehaviour script and open it. Here is an example script to update the agent's target when a GameObject gets close enough.

public class TargetCheck : MonoBehaviour
{
    public GameObject target;
    public float range = 2f;
    private BadgerComponent _agent;

    private void Awake()
    {
        _agent = GetComponent<BadgerComponent>();
    }

    private void Update()
    {
        var blackboard = _agent.Agent.Blackboard;
        if (target == null)
        {
            // Exit if not assigned or missing reference exception.
            blackboard.Set<GameObject>("Target", null);
            return;
        }

        // Distance to target GameObject.
        var distance = Vector3.Distance(transform.position, target.transform.position);
        bool inRange = distance <= range;

        // Set target if in range, otherwise null.
        blackboard.Set<GameObject>("Target", inRange ? target : null);
    }
}

Attach the targeting script to the agent and assign the player GameObject to the "Target" field.

Pressing "Play" will now have the agent chasing after the player when it comes in range.

Last updated