How to make AI agent

This time we will use AgentController and Behaviour Tree to explain how to make AI agent to chase players.

Operating environment

This tutorial is created in the following environment.

Unity 2019.4.8f1
Arbor 3.7.0

Depending on the version, please understand that there are parts different from the explanation.

Preparation of tutorial project

Create project

First of all, please create a project for the tutorial.

Project Name ArborTutorial05
Mode 3D
Assets to import
  • Arbor 3: FSM & BT Graph Editor
  • Characters(Standard Assets)

For information on creating projects and importing Arbor, please refer to “Preparation for using Arbor”.

We will also use Characters of Standard Assets this time, so do not forget to import.

Unity 2018.2.0f2 or later

For Unity 2018.2.0f2 and later Standard Assets has been changed to download from the Asset Store.

  • After creating the project, select “Window / Asset Store” on the menu and display the AssetStore window.
  • Search “Standard Assets” and select.
  • To browse on a web page, click here.
  • Download & import assets.
  • In the Import Unity Package dialog, click the “Import” button with all assets selected.

If an error occurs in SimpleActivatorMenu.cs

In the latest version of Unity, a compile error occurs in “Assets / Standard Assets / Utility / SimpleActivatorMenu.cs”.

This script will not be used this time, so delete SimpleActivatorMenu.cs.

Create Scene

Placement of 3D map

First, we will place a 3D map.

  • Drag “Assets / Plugins / Arbor / Examples / Common / Prefabs/FloorObjects Prefab” from the Project window.
  • Drop into the Hierarchy window.

Save Scene

Save the scene once.

  • Create if there is no “Assets/Scenes” folder in the Project window.
  • Select “File > Save As” from the top menu.
  • Save it as “AgentScene.unity” in the Scenes folder.

Bake NavMesh

  • Select “Window> AI> Navigation” from the top menu to display the Navigation window.
  • Select “Bake tab” in the navigation window.
  • Click the “Bake button”.

  • Save over the scene.

Create Player

Placement of player

Players will use the ThirdPersonController prefab of Standard Assets as they are.

  • Drag and drop “ThirdPersonController” prefab in “Assets / Standard Assets / Characters / Prefabs” folder to Hierarchy.
  • Change GameObject name to Player.
  • Change Position of Transform to (-5, 0, -5).

Camera settings

Let the camera watch Player.

  • Select MainCamera object.
  • Change Position of Transform to (0, 10, -30).
  • Camera ‘s Field of View changed to 40.
  • Added “Arbor > ArborFSM” from Add Component button.

  • Open the Arbor Editor with the Open Editor button.
  • Right click on the graph, select “State creation”, change the state name to “Look At Player”.
  • Added “GameObject / LookAtGameObject” from “Add Behavior” of Look At Player state.
  • Change Target to Player object.
  • Check Apply Late Update.

Create Agent

Placement of character for agents

Place the character.

  • Drag and drop in “Assets/Standard Assets/Characters/ThirdPersonCharacter/Models/Ethan model” to the Hierarchy window
  • Select the Ethan object in the Hierarchy window.
  • Change Position of Transfrom to (5, 0, 5) in the Inspector window.
  • Changed Controller of Animator to “Assets/Standard Assets/Characters/ThirdPersonCharacter/Animator/ThirdPersonAnimatorController”.
  • Add NavMeshAgent by selecting “Navigation / NavMeshAgent” from “Add Component button”.


AgentController is a component built into Arbor that moves in NavMeshAgent and controls Animator.

You can move it on ArborFSM or Behavior tree using Agent script.

ArborReference : AgentController

Add AgentController

  • Select the Ethan object in the Hierarchy window.
  • Add AgentController by selecting “Arbor > AgentController” from “Add Component button” in Inspector window.

AgentController settings

We will set AgentController.

  • Change Agent to NavMeshAgent in Ethan object.
  • Change Animator to Animator in Ethan object.
  • Change Speed Parameter to Forward.
  • Change Speed Damp Time to 0.1.
  • Changed Turn Parametr to Turn.
  • Change Turn Damp Time to 0.1.

Create AI

I will create an AI for the agent.

Add BehaviourTree

  • If you do not select an Ethan object, select it in the Hierarchy window.
  • From the “Add Component Button”, select “Arbor > BehaviourTree” and add BehaviourTree.

Action to patrol Waypoint

Add an action to patrol Waypoint.

Create Waypoint

First, add Waypoint to the scene.

Waypoint is a component that manages the route point placed in the scene.

ArborReference : Waypoint

  • Select “Arbor > Waypoint” from Hierarchy’s + button to create a Waypoint object.
  • Create 4 empty GameObjects below Waypoint.
  • Changed names to “Point 1” “Point 2” “Point 3” “Point 4” respectively.
  • Change icon of each Point object to label type.
  • Point 1 to 4 is set to Points of Waypoint.
  • Change Position of Point 1 to (10, 0, 10).
  • Change Position of Point 2 to (-10, 0, 10).
  • Change Position of Point 3 to (-10, 0, -10).
  • Change Position of Point 4 to (10, 0, -10).

Looking from the top in the scene window it looks like this.

Add AgentMoveOnWaypoint action

The AgentMoveOnWaypoint action is an action to move the Agent along the Waypoint.

ArborReference : AgentMoveOnWaypoint

  • Click the Open Editor button from Behavior tree of Ethan object and open ArborEditor.
  • Drag the slot at the bottom of the Root node and drop it under the Root node.
  • Select “Create Action” in the displayed menu.
  • Select “Agent > AgentMoveOnWaypoint”.

AgentMoveOnWaypoint Settings

  • Change AgentController to AgentController in Ethan object.
  • Change Speed to 0.5.
  • Change Waypoint to Waypoint in Waypoint object.
  • Change Type to Cycle.
  • Change Stopping Distance to 0.5.

Play Check

Let’s Play and check it.

Regardless of the position of the player Ethan keeps circling the fixed route.

Action approaching Player

Let’s approach if Player is near to some extent.

Add Selector Composite

Selector is a composite that executes child nodes in order from the left until it finds a child node that returns “success”.

ArborReference : Selector

  • Moved AgentMoveOnWaypoint action to lower right.
  • Drag the slot at the bottom of the Root node and drop it under the Root node.
  • Select “Create Composite” in the displayed menu.
  • Select “Selector”.

Connect Selector to AgentMoveOnWaypoint

  • Drag the slot under the Selector node
  • onnects to the top slot of the AgentMoveOnWaypoint node.

Add AgentMoveToTransform action

AgentMoveToTransform is an action to move the Agent closer to Target.

ArborReference : AgentMoveToTransform

  • Drag the slot at the bottom of the Selector node and drop it to the left of the AgentMoveOnWaypoint node.
  • Select “Create Action” in the displayed menu.
  • Select “Agent > AgentMoveToTransform”.

AgentMoveToTransform settings

  • Change AgentController to AgentController in Ethan object.
  • Change Speed to 0.6.
  • Change Stopping Distance to 2.
  • Change Target Transform to Player object.

Creation of distance check decorator

Create a decorator to decide whether it approaches according to the distance to the player.

  • Select the Assets folder in the Project window.
  • Select “Arbor / Behavior tree / Decorator / C # Script” from “Create” button.
  • Decided by setting the script name to “DistanceCheck”.
  • Open DistanceCheck file in script editor.
  • Enter the following code.

Distance judgment by DistanceCheck

Let’s set DistanceCheck to run AgentMoveTransition node by distance.

  • Click the AgentMoveToTransition node setting icon.
  • Select “Add Decorator” from the menu.
  • Select “Scripts> Distance Check” from the decorator selection window.
  • Changed AbortFlags to Everything.
  • Change Target to Player object.
  • Change Distance to 10.

Play Check

Let’s play and check it.

  • As you approach the Ethan object within 10 meters by manipulating the player, Ethan also approaches the player.
  • Conversely, when the player leaves 10 meters, he will return to patrol.

In this way, it is better to remember that nodes are executed in order from the left, decorators judge whether they can be executed, and control interrupts and interrupts.

Next step

In this time I created an AI that approaches Player using AgentController and Behaviour Tree.

In an actual game, most cases take actions such as attacking after getting close to Player to some extent.

I will finish this tutorial as a task for you “How to make Ethan a better AI”.

Attack action tips

  • When AgentMoveToTransform approaches StoppingDistance, execution is terminated as “Successful”.
  • The Sequencer composite executes child nodes in order from the left as long as the child node returns “success”.
    ArborReference : Sequencer
  • You can also add a decorator to the composite node, and if the AbortFlag has a Self flag, execute the child node as long as the decorator condition matches.
  • Actions not only create your own scripts but you can also use the SubStateMachine action to run the state machine as an action.
    ArboReference : SubStateMachine
  • With ArborFSM, you can use EndStateMachine to return the result to the parent graph.
    ArborReference : EndStateMachine
  • How to make an attack by a state machine will be helpful as “Let’s make an enemy of 2DSTG”.
    *However, Ethan has no attack motion, so you need to get it from the asset store.

Privacy Preference Center

Your Privacy

This site uses cookies to display contents and advertisements tailored to users, provide social media functions, measure advertisement impressions and clicks. We also gather information on users' site usage status and provide them to social media, advertisement distribution and data analysis partners. Each partner may use this information in combination with other information that the user provided to each partner or other information gathered when the user used the service of each partner.

Analytics, Ads