Creating Coin Distraction Feature in Unity (Part 3: Send AI to Coin)

Moving the guards to the coin

Rusben Guzman
2 min readDec 1, 2021

Objective: Create a code to move the guards to the coin

First I assigned the tag “Guard” to each guard in the scene.

The idea is to create a function in the player script called “SendGuardToCoin”. This function receives as a parameter the position of the coin. We have to get all the guards and assign the new destination.

To get all guards we can use the function GameObject.FindGameObjectsWithTag(“Guard”). Then I used a foreach loop to iterate each guard, get the GuardAI script and call a new function called “MoveTo” that receives the position of the coin as a parameter.

In the GuardAI script I create the function MoveTo. This function sets the walk animator parameter as true to activate the walk animation and sets the nav mesh agent destination to the coin position.

And that’s all!!! Now the guards walk to the coin when it is tossed.

--

--

Rusben Guzman
Rusben Guzman

Written by Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

No responses yet