My journey becoming a Unity game developer: Coin Distraction-Use AI to make the guards find the coin dropped
Objective: Have security guards find the coin dropped navigating through the NavMesh. Also, switch from the Walk animation to the Idle animation when they find the coin.
Let’s begin by selecting all 3 Security Guards and making sure they have the same Tag name which in our case will be Guard1.
In the GuardAI script, create a new variable Bool type name coinTossed and set it to False. This will be used to check if the coin has been tossed. Inside the Update() function, we want to check not only IF the waypoints are more than nothing and the waypoints exist. We also want to check IF the coin hasn’t been tossed. If so, the guards will go to their waypoints as previously coded.
In the Coins script, create a new method name SendAItoCoinSpot() that will pass in a Vector 3 coin position as its parameter. Create a new Game Object type Array variable name guards to find game objects with the tag “Guard1” as this will be how we find all the security guards.
Now use the ForEach loop to find each guard in the guards Array variable. Create a new variable inside the ForEach loop that will get the NavMeshAgent component of each guard name currentAgent. Also, create another new variable to get the GuardAI script component name currentGuard.
Use the currentGuard variable to assign the coinTossed variable inside of the GuardAI script to True. Then set the security guards destination to find the coin position.
In the Update() function under the IF Raycast condition, we will call the SendAItoCoinSpot() method passing in the hit information point where the right-click button drops the coin. This method will be placed after the coin is instantiated and the coin’s audio plays.
When we play the game and right-click at a point on the screen, the security guards will move to the coin’s position. However, the guards will stay in their Walk animation pushing each other out of the way trying to get as close to the coin as possible. Also, when the coin is tossed, if the guards are in their Idle animation they will move to the coin in their Idle animation regardless if they are suppose to be walking along their paths.
To keep the guards from walking non-stop around the coin, create another new variable name anim to get the Security Guard’s Animator controlling their animations. Also, use the guard’s coin position variable to pass in the coin’s position that is used from the hit point information.
Now set the guards coinTossed variable to True indicating the coin has been tossed. Then set the guards destination to the coin’s position. Last, use the SetBool method with the anim variable to set the Walk animation’s name to True which will activate the Walk animation when the coin is tossed.
Back in the GuardAI script, we need to create an Else statement that will use a distance variable to get the distance between the guards positions and the coin position. IF the distance is less than 1 meter, use the Animator’s SetBool method to set the Walk animation to False.
The guards will now walk to the coin when it’s tossed, but they still keep pushing each other out of the way for the coin.
Inside of the Else statement, have the guard use the stoppingDistance function and set it to around 5 or something to your liking that will make the guards stop a certain distance away from the coin. We hard coded the stoppingDistance for the guards instead of using the NavMeshAgent in the Inspector because earlier when creating the waypoints, the guards were not stopping directly on the waypoint itself. Therefore, we will keep the default values for the guards to move along their paths.
The guards will now walk to the coin when it’s tossed, and stand around 5 meters away from the coin.
Next time we will work on the Coin Toss Animation of Darren throwing the coin.