Tutorial: Unity Follow AI

josé edson
5 min readSep 14, 2019

--

Today we are going to learn how to make an entity of a game follow another one, you can use this element in many projects in different ways. Let’s start importing and preparing the animation’s sprites of the character.

1º STEP: CREATE A NEW PROJECT

  • Open the Unity3D (version 19.2)
  • Click in File -> New Project
  • Select the local for your project (it’s recommended a folder dedicated to keeping all of your Unity projects)
  • Click on Create.

2° STEP: IMPORT YOUR ASSETS

4° STEP: MAKE THE PLAYER

You can do the download the characters on-line, with the UnityStore. Select the cube on the Hierarchy and rename with “Player”. Reset the transformation. Now we need physics. Leave the player subject to the physics’ laws clicking on Add Component under the Inspector panel selecting the player.

Add physics -> Rigidbody. Leave all the default settings.

You’re going to notice that every object come with a variety of “components” with them, you can see the Inspector. Every cube, sphere, etc, there’s a component called “collider” this component prevent two body’s in the same space. If you turn off the collider, the object will be like a “ghost” and can pass through other objects. You can turn on or turn off the components selecting and deselecting the box next to the name of the component.

5º STEP: MAKING THE PLAYER’S MOVEMENT

Select the player on the Hierarchy window Minimize the components that you don’t want to see open on the Inspector clicking on the arrows down left each component’s name. This will clean up your space a little.

Click on Add Component under the Inspector’s window. Select New Script, rename as your preference and click on Create and Add.

To keep the files organized, open the folder Actives on the window Project. This opens the app to a program called Visual Studio (Our Case) or Visual Code.

That should have already two sessions include in your code for pattern: Void Start () and Void Update (). Start when the object begins in the game, and the update is performed continuously while the object is in the game. Let’s add one third, called FixedUpdate, to deal with protocols related to physics or only add the name Fixed beside of the Update. Should be like this:

void FixedUpdate () {

}

Before we put the commands, we need to name the variables. This is made in the top of the page, inside the keys after Public Class “the name you gave”: Monobehaviour, but before the function void Start (). For the movement, we use one variable called “Speed”, that we can adjust to determine the character’s velocity in the arena.

Declare the type of the de variable (floating) and the name (speed) in this way:

public float speed;

Declare also the variable Rigidbody that will apply the component’s physics

private Rigidbody RB;

The semicolon tells the program this the end of the code line. You’ll receive an error if you forget the semicolon at the end of every coding line, so don’t forget it!

On FixedUpdate, declare two more floats, move horizontal e move vertical. They take values depending on the user’s keyboard commands, and the FixedUpdate update all the frames.

float moveHorizontal = Input.GetAxis(“Horizontal”);

float moveVertical = Input.GetAxis(“Vertical”);

Also inside the FixedUpdate, create a new Vector3, a kind of three dimensions variable to move objects in the 3D space. This will take over the user’s input value for the right and left the movement.

save the file CSharp (CTRL + S) and go back to Unity.

Go to the panel Inspector of the player and look the movement’s script that you has just made. That should have a box for your public variable, velocity. You can change the value of the public variables using the Inspector.

6 Step: Make one entity follow the player

Create/Add a new object (can be an asset or one cube for example), select the object in the hierarchy, click on Add Component in the bottom part of the Inspector’s window. Select New Script, name the script and Click on Create and Add.

Before we can insert commands, we need to declare variables. This is done in the higher part of the page, inside the keys before Public Class TheNameYouGave: Monobehaviour, but before the function void Start (). For the movement, let’s use the variables:

In Void Start, put the following code to add the animation component to the object in the script that will be used.

Save the file CSharp and come back to the Unity

Go to the Inspector panel from the player and take a look to the script you’ve just made. There should be two boxes for your public variables: “Player” and “anim”. You may click and move the “Player” and the animation to their following “boxes”, like is showing below:

Click on the PLAY button in the top, in the middle of the screen you’ll see this result:

Equipe:

Ana Beatriz Sales da Nobrega — anabibisales@gmail.com (Translation)

Esther Vitoria Bezerra de Brito — esthervitoria973@gmail.com (Organization)

Italo Lino Barbosa Junior — italo.lino@nave.org.br (Organization)

José Edson Amorim Sebastião — joseedsonamorim@nave.org.br (Text, Images and codes)

Laura Medeiros de Souza — laura.ms@nave.org.br (Translation)

Wallace Ribeiro da Silva — wallace.ribeiro.ds@nave.org.br (Organization)

--

--