top of page

Extra assignment

For this assignment, you'll be creating a more complex behavior. You will also program a new node that changes the color of the agent’s material.

The end result

You will create a behavior that approaches a target when the agent can see it.

 

To indicate whether it can see its target and is currently chasing it, it changes its color.

Red for when in pursuit, and gray for when idle.

extra-assignment-demo.gif

Programming a node

To program a completely new Action node, we will first need to create a node in the Unity editor.

Creating a node

Create a node with the following steps

  1. Import the Unity Package called “Script Templates” in the Behavior Trees folder

  2. Restart Unity

  3. Create a new Action node using the Create menu by going to 

    1. Assets

    2. Create

    3. Bob Jeltes

    4. AI

    5. Behavior Trees

    6. Action Node

  4. Enter the name “ChangeColor”

  5. Open the script

The code

Let's go over a few pointers to get the ChangeColor node to work.

The OnStart and OnStop methods

These methods are called when the methods are first or last called. For the ChangeColor node, we will not be using them.

 

The OnUpdate method

The OnUpdate method is called every time a node is updated from the behavior tree.

The behavior tree executor passes a reference of itself to the behavior tree that it executes. This reference is passed down to every single node that is executed so that information such as components can be accessed from any node in the behavior tree.

 

Retrieving the color

First we'll need an integer for the ID of the color variable that we will reference.

public int ColorID;

We then use this color variable in the OnUpdate method.

TypedVariable<Color> colorVariable = behaviorTreeExecutor.GetColor(ColorID)

 

Storing the color retrieved from this function will allow us to access it later.

 

Renderer

Second we'll need a renderer of which we are to change the color.

To change the color of the renderer that is attached to the behaviorTreeExecutor parameter, we will use the following:

Renderer renderer = behaviorTreeExecutor.GetComponent<Renderer>()

Using the stored color from the GetColor function we used earlier, we can now assign this color to the Renderer component that we retrieved using the GetComponent<Renderer> function.

Assuming that we're working with a 3D object, we can assign the color to the renderer.material.color property like so: 

targetRenderer.material.color = colorVariable.value;

Your OnUpdate function should look something like this. 

// This is called when the node is updated
public override Result OnUpdate(BehaviorTreeExecutor behaviorTreeExecutor)
{

    // Get the color value from the blackboard
    TypedVariable<Color> colorVariable = behaviorTreeExecutor.GetColor(ColorID);

    // Get the renderer to change the color of
    Renderer renderer = behaviorTreeExecutor.GetComponent<Renderer>();

    // Set the renderer's material color
    renderer.material.color = colorVariable.value;
    return Result.Success;
}

The ChangeColor node script

Below you can find the full script for the ChangeColor action node. For completeness and to reduce errors, the node returns a Failure when the color variable or renderer can't be found.

using UnityEngine;
using BobJeltes.AI.BehaviorTrees;
using BobJeltes.AI.BehaviorTrees.Nodes;

public class ChangeColor : ActionNode
{
    public int ColorID;

    // This is called when the node is first updated
    public override void OnStart()
    {
       
    }

    // This is called when the node stops being updated
    public override void OnStop()
    {
       
    }

    // This is called when the node is updated
    public override Result OnUpdate(BehaviorTreeExecutor behaviorTreeExecutor)
    {

        // Get the color value from the blackboard
        TypedVariable<Color> colorVariable = behaviorTreeExecutor.GetColor(ColorID);
        if (colorVariable == null)
        {
            return Result.Failure;
        }

        // Get the renderer to change the color of
        Renderer renderer = behaviorTreeExecutor.GetComponent<Renderer>();
        if (renderer == null)
        {
            return Result.Failure;
        }

        // Set the renderer's material color
        renderer.material.color = colorVariable.value;
        return Result.Success;
    }
}

 

Now that the node has been created, you're ready to start creating the behavior.

Required nodes

To get you started, these are the nodes you'll need:

  • Repeat

  • Selector

  • Sequence (2)

  • CanSeeObject

  • SetNavMeshDestination

  • NavMeshStart

  • NavMeshStop

  • ChangeColor (NEW) (2)

Variables

You will also need to create a variable for the target, and two color variables.

Scene setup

The behavior tree makes use of a NavMeshAgent component. You will need to add it to the agent, and set up the NavMesh in the scene. You can learn more about how to do that here: Unity - Manual: Building a NavMesh (unity3d.com)

Good luck!

bottom of page