> For the complete documentation index, see [llms.txt](https://badgerhtn.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://badgerhtn.gitbook.io/documentation/guides/chasing-a-player.md).

# 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".&#x20;
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.

<figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FneGHXSmBNdJgdFhIFxP2%2Fimage.png?alt=media&amp;token=9e51d23d-de63-42a1-b588-46dc8c1286ba" alt=""><figcaption><p>A wander behavior.</p></figcaption></figure>

### 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.&#x20;
2. Add a BadgerAgent component to the GameObject and in the "Asset" field select the newly created "Wander" tree.

<figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FGqVv9TPevRBsPX9aZRc3%2Fimage.png?alt=media&amp;token=be1f01e3-2951-491d-a2a4-3d6e4abca763" alt=""><figcaption><p>Agent with a Badger component.</p></figcaption></figure>

3. 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".

<figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FYJRhZBxJyAa6bcSZg01P%2Fimage.png?alt=media&amp;token=68e24c45-57ae-42bd-bad4-deb6413b4afc" alt=""><figcaption><p>Chase blackboard.</p></figcaption></figure>

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

<figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FTCYsR2feYJnxhFqwGKVJ%2Fimage.png?alt=media&amp;token=2c84a003-b02c-4eb5-8d14-92c94ecf7d75" alt=""><figcaption><p>Assign a blackboard.</p></figcaption></figure>

4. 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.\ <br>

   <figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FWEN3wCbC8yhASjvhGziV%2Fimage.png?alt=media&amp;token=bf98a7ca-2e81-441d-af33-ca99af870733" alt=""><figcaption><p>Move to target.</p></figcaption></figure>
5. 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.<br>

   <figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FPDqMonNG7Sr28Oou1XlV%2Fimage.png?alt=media&amp;token=66733f96-c0a5-412b-a834-4eb9b606f8af" alt=""><figcaption><p>A wander sub-tree.</p></figcaption></figure>
6. 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".<br>

   <figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FOsbR0cAT1Rua9UoABgq1%2Fimage.png?alt=media&amp;token=a141aea8-a6ca-46a3-92ac-916993ce62b7" alt=""><figcaption><p>A chase behavior.</p></figcaption></figure>

### 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.<br>

<figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FE8u8la1hMXNCB24bASnr%2Fimage.png?alt=media&amp;token=1183fdf6-86ba-48fe-bb39-f7ba58ddc536" alt=""><figcaption><p>Targeting component.</p></figcaption></figure>

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

<figure><img src="https://3724840250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwrNt0ljEuQ5gsxIfJNfv%2Fuploads%2FPSXCFPGNGhSyyjMzchwk%2Fimage.png?alt=media&amp;token=d21936f4-f4a2-43d9-b220-997e9e50eb3d" alt=""><figcaption><p>Chasing the player GameObject.</p></figcaption></figure>
