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

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store