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 5.4.0f3
  • Arbor 3.0.2

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 “Getting Started: Preparation for using Arbor”.

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

Create Scene

Placement of 3D map

First, we will place a 3D map.

  • Drag and drop the “FloorObjects” prefab in “Assets / Plugins / Arbor / Examples / Common / Prefabs” to the Hierarchy window and place it.
GIF

Save Scene

Save the scene once.

  • Select the Assets folder in the Project window and select “Folder” from “Create button”.
  • Decide the folder name as “Scenes”.
  • elect “File / Save Scene” in the menu.
  • Save it as “AgentScene.unity” in the Scenes folder.

Bake NavMesh

  • Select “Window / Navigation” on the menu to display the Navigation window.
  • Click “Bake button” in Navigation window.
  • 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 Transform to MainCamera object.
  • Change Target to Player object.
  • Check Apply Late Update.

Create Agent

Placement of character for agents

Place the character.

  • Drag and drop “Ethan” model in “Assets / Standard Assets / Characters / ThirdPersonCharacter / Models” 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 Animator’s Controller to “ThirdPersonAnimatorController” in “Assets / Standard Assets / Characters / ThirdPersonCharacter / Animator”.
  • Add NavMeshAgent by selecting “Navigation / NavMeshAgent” from “Add Component button”.

AgentController

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 / Behavior Tree” and add Behavior Tree.

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 Create 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.
    GIF
  • 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 / Agent MoveOnWaypoint”.
GIF

AgentMoveOnWaypointの設定

  • 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.
GIF

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

  • 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”.
GIF

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”.
GIF

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 gear icon of the AgentMoveToTransform node and select “Add decorator”.
  • Select “Scripts / DistanceCheck”.
    GIF
  • 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.
GIF

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 ArbotFlag 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”.