Coin Distraction Part 2

addam davis
3 min readSep 21, 2021

Objective: Add function of guards investigating the coin.

Now the the player has one coin they can throw to distract the guards, we now have to make the guards get distracted. To begin open the Player’s script and we need to create an array for the guards.

We need to create a method for sending the enemies to the coin. This method is also going to need a Vector3 parameter to give the guards the exact location of the coin.

You need to make sure all the enemies you want to be able to be distracted have the same tag. This way we can us the Find game object with tag. With every enemy was find with the tag we want to run a for each loop.

What do we want to do with each guard we find. We want to grab their NavMesh, Script, and Animator.

With the NavMesh we want to set their location to the vector3 we pass in when calling this method.

Next with the GuardAI script we want to set a coin tossed bool to true and set a coin vector3 position. We will do more with those in just a minute.

We want to set the animation bool “walk” to true, this way when the enemies head to the coin they will be using their walking animation.

In the enemy’s script we have the if statement for their movement, we need to add the parameter for coin tossed to be false.

If the coin tossed is true you need to create an else statement. Inside this you need to calculate the distance between the enemy and the coin using the coin position we passed in earlier. Once the distance is small enough you can turn your animation bool to false, this will leave the enemies to stand with the coin.

Now your enemies will be sufficiently distracted with the drop of a coin.

Do not be afraid to experiment with the stopping distance for your enemies, and I’ll see you in the next tutorial!

--

--