Setting up an agent

This tutorial will show you how to set up an AI agent with the AIMotor script provided in the SimpleSteer package. You can find the example project under Documentation. You can click all the images to enlarge them. First let’s take a look at what is inside the package.

There are two folders, one called SimpleSteer and the other called Scripts. In the Scripts folder you can find the ArrayTools.cs script. The original version can be found on the Unity wiki. The provided version has three new methods; AddItem, FindItem, and ClearArray. This script is used by some behaviours so it should be alway in the project that uses SimpleSteer.

The SimpleSteer folder has three sub-folders; AISteeringComponent, Behaviours, and Utilities. In the AISteeringComponent folder you’ll find the AIMotor.cs script. This scripts can be added to the agent as a component and it is what actually makes use of the individual behaviours. It blends the returned acceleration of the behaviours that are built to return acceleration and uses the returned rotation of those that return rotation. It provides public methods for a decision-making component so that it can enable and disable the behaviours. This is a fairly useable version of a steering component, but it can be modified, or used as an example for completely new variants.

There is no decision-making component in the package, but examples for that will be included in the example project, I will publish here.

Agent set up with all the necessary components.

The next folder is Behaviours. As the name sais it contains the actual behaviours. These ae not derived from MonoBehavour so they can not be added to gameobjects. You can read through them to see how they work and to get an understanding of the public variables since you will need those to fine tune the behaviours. I will explain those in later chapters of the documentation though.

The last Folder contains helper scripts. SteerOutput is the returned struct of the behaviours and SteeringSensor is used by the Separation and SphericalAvoidance behaviours. More on these in later chapters.

Rigidbody constraints set.

So let’s set up an agent. We add capsule to the scene, and add a Rigidbody component to the capsule gameobject. In the Rigidbody component, under Constraints we check Freeze Rotation X, Y, and Z. Now we can add the AIMotor Component from the AISteeringComponent folder. At this point we could start using the agent for path following, but we would get some error warnings and SphericalAvoidance and Separtation would not work. So we add the steering sensor to the agent.

Let’s create an Empty gameobject and make it the child object of the capsule. I named these Agent and SteeringSensor. The SteeringSensor needs some sort of trigger volume. We will simply use a Sphere Collider for now. So we add the Sphere Collider component and check the Is Trigger check box. I used it with a radius of 5 for the example (image).

The correct set up for the layer collision matrix.

Now in the Tag Manager we need to create two new layers (image). I called them SteeringSensor and SphericalObstacle. In the Layer Collision Matrix of the Physics Manager we have to set up the SteeringSensor layer to only interact with the SphericalObstacle layer. The only thing left to do is to change the layer of the SteeringSensor gameobject to “SteeringSensor” and the Agent to “SphericalObstacle” (image). If we do it in this order we will get a pop up asking if we want to change the layer for the children of the Agent as well (image). We don’t.

The agent is ready to go at this point but it lacks any kind of motivation to actually go anywhere. That is being handled by the AIBrain script. This is included in the example project and is well commented. It is a very primitive implementation of a decision-making component though.

So that is all for starters. I encourage you to take a look at the example. In the next chapter I will write about the details and what should be considered when tweeking the behaviours, or what I have experienced so far.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s