Implementing a Distraction Mechanic
Explaining how I created a distraction mechanic in our stealth game.
Objective: Code in a mechanic that allows our player to distract the guard AI.
Distract with a Coin
The first thing I did to get this done was when I right-click on a position on the floor, a coin prefab will be instantiated at that point, along with a little sound effect. This is the main chunk of code I added to my Player script to get this to work:
This gave me the following result:
Next, I worked on sending the AI to the position the coin dropped. The main code I used to get this done is split between my Player class and the GuardAI class. In my player class, I call the following method when the coin is dropped:
Notice how I am mainly just calling a method called CoinTossed in my guard agents. That’s this code here:
This code, plus a bit more in the Update method of my GuardAI class, gave me the result we see below:
Notice how the guards are distracted and moving towards the coin in the image. Just like that, we have a decent distraction mechanic!