Distracting Guards with Coin

Giving the Guard AI a new waypoint upon Instantiating a coin

Simon Truong
4 min readAug 16, 2021

--

Now that we are able to Instantiate infinite amounts of coins, it is time to end the fun and limit the coin spawn to 1. Then we can write some codes to have all 3 Guards to be alerted by the presence of the coin and move towards it. We will need both the Player script and also the Guard Ai script to complete this feature.

The Player Script

Limiting the coin to one:

To limit the coin toss to only once chance, we simply add an true or false Boolean during the right click function. By default the Boolean should be set to false, and the IF statement which checks the mouse button click should also check if the Boolean is still “false”. Only when the status is still false can we follow up with the rest of the code which instantiates the coin.

During the Instantiation, we set the Boolean to true. Since there is no other method to set the Boolean back to false again, this status will always stay true. Therefore we won’t be able to instantiate another coin again due to the limitations of the IF statement.

Sending AI to Coin:

Next, we need to create a new method which controls where the Guards AI will travel to. Since this new method needs access to the “hitInfo” data from the Ray cast for the coin. We need to create an variable to contain the hitInfo data. This can be done by giving the method and Vector3 variable of “coinPos”, then back in the Ray Casting, we can have that method access the hitInfo.point.

In this method, we need to gain access to the Nav Mesh Agents of all the Guards available. To make the process streamlined we can create an Game Object Array which scans and stores targets with an certain “Tag” name.

Storing all the Guard game objects into an Array using a Tag name search

Using an for each loop, we set every guard variable in the array and get the Nav Mesh Agent, their AI script and their Animator.

The Guard Ai Script

Stopping the waypoint loop and focusing on the coin:

In the Guard Ai script, once we instantiate the coin prefab we need to stop the Guard AI from looping to the next waypoint and just solely focus on the newly instantiated coin.

We first start by creating an Boolean to check whenever the coin has been tossed or not. By default it is set to false and when it is false, the Guard Ai will continuously loop back and forth their preset destinations.

Setting the If statement to check whenever the coin toss is true or false

Next we add in a new ELSE statement which basically handles whenever the coin toss statement is true. In this ELSE statement we check the distances between the current position of the guard and the newly instantiated coin position. If set distances is less than a certain variable we set the animation to idle.

The Player Script

Setting new destination:

Back in the Player script, we are going to continue back in the new method. Now that we have a new Boolean in the guard AI script, we can access that in the Player script.

First, set the coin tossed Boolean to true so the Guard Ai waypoint looping can stop. We also need to pass the coin position data to the Guard script, so we have the Ray casted coin position = the Guard Ai new location coin position.

Then we are going to set the new destinations to the coin position, and finally set the animation of the guards to walk.

The final script should look like this

Save and head back into Unity. Now when you test the game out, the Guards should be distracted with the newly instantiated coin.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.