Agent Based Modeling and Behavior Bricks
Module Summary
In this tutorial we will use Behavior Bricks (a plugin for character animation) in Unity to create 2 simple agents: an interactive Player (moved around using mouse clicks), and an Agent that wanders around, and pursues the player when it sees them. We’ll cover the following core concepts:
- Setting Up Behavior Bricks
- Creating A Behavior
- Editing Behavior Parameters
- Composing Behaviors
- Conditions & Perceptions
Conceptual Introduction
Behavior Bricks is a character animation plugin for Unity the gaming engine. What is quite useful about Behavior Bricks and character animation apps like it, is that it allows us to program behavior of an agent with high level programming. Behavior Bricks uses a node based Visual Programming Language (akin to Grasshopper) to program things like character movement, following, or other actions based on other triggers. This will be particularly useful for modeling behaviors in relation to one another in an agent based model.

behavior bricks user interface
Tutorial
1 — Setting Up Behavior Bricks
- Start a new project 3D
- Get Behavior Bricks from the Unity Asset store.
- Go to Window → Asset Store
- Click Search → and search for “Behavior Bricks”

find behavior bricks in the asset store
- Select the pink Import → this brings up a list of all of the assets in Behavior Bricks.
- Select Import → this will load Behavior Bricks into your current project

import behavior bricks
- To get out of the Asset Store click the Scene tab. You’ll now see Behavior Bricks in your project under the Assets Folder.

get out of asset store
- Create a plane GameObject →3D → Plane.
- Right Click it and Rename it to
Floor.

creating a plane in unity
- Set the position to
(0, 0, 0), and the scale to(5, 1, 5)so it covers a bigger area. - Check the
Staticcheckbox near the Game Object name in the Inspector.

set position and make static
- Move the main camera to
(0, 20, -30), and set the rotation to(45, 0, 0)in order to fit the plane into the view.

move the camera
- Create a sphere for the player.
- Rename it to Player, and place it in (0, 0.5, 0) so it will be over the floor.

make a sphere player
- Create a cube for the agent.
- Rename it to
Agent, and place it in(20, 0.5, 20). It will be quite far away from the player, near the plane limits.

make the cube agent
- Create three new materials,
Green,BlueandRedand use them for the floor, player and agent respectively. - To create a new material go to Assets → Create → Material.
- Right click to rename the material
Green. - Use the eyedropper to select a color. Then drag and drop the material to assign it to a game object.
- To quickly duplicate a material select it and use ctrl+d (Windows) cmd+d (Mac). This is a good time to save.

apply materials
- Create the navigation mesh that will be used by both the agent and the player for pathfinding.
- Select the floor, go to Window → AI → Navigation,
- go to the Bake Tab and press the Bake button.

navigation mesh
2 — Creating A Behavior
- We’ll create a wander behavior for our Agent.
- Go to the Behavior Bricks menu Window → Behavior Bricks →Editor. That will open the Behavior Bricks Editor.

create a behavior
- The first step is to create a new behavior tree by clicking the
Create new behaviorbutton in the upper part of theCollectiontab and naming it “Wander”.

create a behavior wanderer
- Once the new behavior asset has been saved it will appear in the
Behaviorslist and will be opened as a new tab in the behavior graphical editor, showing an initially empty canvas.
3 — Editing Behavior Parameters
- Make the agent move to a position (-20, 0.5, 20)
- In the canvas for the
Wanderbehavior right-click to add a new node and choose the action MoveToPosition from the drop-down menu. You can type move in the search box to quickly find the action.

make the agent move
- Click the new
MoveToPosition' node → **select** theNode` tab in the Behavior Bricks inspector. - This tab shows the properties of the selected node in the canvas. In this case you will find the input parameters, that constitute the information the action needs to know how to proceed.
- Change the dropdown from
CONSTANTtoBLACKBOARDand change the target name towanderTarget. - All changes are automatically saved in your project so, once done, you can close the Behavior Bricks editor.

- Connect the Behavior to the “Agent” Game Object
- Select the
Agent→ clickAdd Componentfrom the inspector tab → select theBehavior Bricks - Behavior Executor.

- From the asset folders, look for the
Wanderbehavior, drag and drop it in the Behavior ExecutorBehaviorvariable where it saysNone (Internal Brick Asset). This links the wander behavior to the selected game object and executes the Behavior Bricks script during the runtime.


- Under the
Behavior Paramschange the values to(-20, .5, 20)

- Launch your project and you will see the
Agentmoving to the left.
Be aware of the Unity warning “The Enemy game object does not have a Nav Mesh Agent component to navigate. One with default values has been added”. This is due to a missing component in the Enemy. Specifically, MoveToPosition action uses the scene nav mesh, which requires a nav mesh agent component. Unity automatically adds it on runtime when missing, and warns about that. You can avoid the warning adding that component beforehand yourself.

4 — Composing Behaviors
- Lets make the behavior more interesting for our Agent by making the
MoveToPositiongo to aRandomposition. - Open
Behavior Bricks Editoragain Window →Behavior Bricks →Editor and make sure you’re in theWandertab. - Right click to add a new node to the canvas for the
GetRandomInAreaaction. - Create a new blackboard input parameter and call it
wanderArea. - Select the previous
wanderTargetas the blackboard parameter where the action will write the selected position.


- In the
Blackboardtab change thewanderTargetfromINtoLOCAL.

- The action marked with an
R(MoveToPosition) is the root of the behavior tree and will be executed in the first place. In fact,GetRandomInAreawill not be executed at all. In order to create a behavior with two actions, we need to compose them.

- Right click on the canvas an add a composite
Sequence Node. A Sequence is a composite node that executes its children in order. - Click the handle from the bottom of the
Sequence Nodeand connect it toMoveToPositionandGetRandomArea, clicking at the top of each node to make the connection.
Our behavior is now a valid tree!
The respective numbers 1 and 2 at the top of these nodes represent the order of execution. If you move the nodes around the numbers, and thus the execution order will change.

Making A Repeat Behavior
- The sequence executes its children once. We want the Agent to continually wander around, so when it reaches the random position, another one should be selected and the Agent should go there.
- Insert a
Repeat Decoratornode into the canvas. This internal node executes its child behavior again and again. - Create a connection from the
Repeatnode to theSequence. Observe how theRmark readjusts itself.

- Close the editor. Remember that behaviors are automatically saved.
- Select the
Agentgame object and observe the behavior executor parameters. You’ll noticewanderAreais still unestablished so the GetRandomInArea does not know the area where the position should be chosen from. Specifically,GetRandomInArearequires a game object with a box orsphere collider.

- Select the
Floorgame object, and add it a Box collider. Set(10, 0, 10)as its size. - Select the
Agentgame object so you can see its elements in the inspector, and drag and drop theFloorinto thewanderAreaparameter.

- Play the scene. Observe the wander behavior of the enemy. It will continually move around to random positions.
- The final version of this
Wanderbehavior is available asDone Wanderin the Behaviors Collection.

5 — Conditions & Perceptions
We want the player to move around using the mouse. This requires three steps:
- Detect the mouse click. This provides us with a coordinate in screen space (2D).
- Convert the click position into a 3D coordinate using a raycast to detect the object under the mouse.
- Ask the player avatar to move to that position.
We’ll use the built-in CheckMouseButton condition that test if the user has just pressed one of the mouse buttons and ends with success in that case.
- Open the editor window and create a behavior with the name
ClickAndGo.

- Add a
CheckMouseButtonnode.

- Add a
FromMouseToWorldaction. This action convert the current mouse position from screen coordinates to world coordinates. It has four parameters:camera: camera used for the translation. Add a new blackboard field and keep the default name,camera.mask: layer mask used for filtering the raycasting. Add a new blackboard field and keep the default name.selectedGameObject: an output parameter where the action will write back the game object under the mouse, if any. We are not interested on it in this behavior, so we will keep it unassigned.selectedPosition: output parameter with the 3D position of the mouse relative to the chosen camera. Add a new blackboard field and keep the default name.

-
Add a
MoveToPosition. Change its target parameter to theselectedPositionfield in the blackboard. -
In the Blackboard tab change the
selectedPositionparameter type fromINOUTtoLOCALbecause it is not relevant for the game object.

- Add a
Sequenceand make those three nodes its children. - Add a
Repeatnode so the sequence loops again and again and the user can move around to different positions.

- Close the editor and add a Behavior executor component in the
Playergame object. Attach to it theClickAndGobehavior.

- Configure the
cameraparameter with theMain Camera, and set themasktoEverything, so the screen-to-world transformation is done using the camera view, and the raycasting considers all objects in the scene.
