Creating Coin Distraction Feature in Unity (Part 3: Send AI to Coin)
Moving the guards to the 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.