Creating an Distraction (part 1)
Using a coin to create noise and Distraction against Guard AI
--
After creating the Guard AI movement and also having virtual eyes that the Guards can use to track the player, we can fully play the game at its intended capacity. Now it is good time to add some some game play features, such as tossing a coin as a distraction for the guards to be preoccupied with. To make this happen, we will required the “Player Script”, a “coin prefab” and also the “coin dropping SFX”.
Player Script
In the player script, we have already written the code for left mouse button click to move the player. Now we want to have the right mouse button click to instantiate the coin prefab along with sound effects.
Start by creating a public Game Object to hold the coin prefab. We are also going to create an Audio Clip handler as the same time so we can play an sound clip we instantiate the coin.
Then in the Update method we are going to create a new IF statement that tracks the Input of the right mouse button. Using “GetMouseButtonDown(1)” we are able to track the input devices.
tip: Remember that zero is for Left click and one is for Right click. Number two is for the mouse scroll wheel.
Next we are going to instantiate the coin prefab by using the Instantiate script. Following the same method as the Left click to move player, we are also going to use the same Ray Cast formula to target the location of where the coin drops.
Within the IF statement we are going to Instantiate the coin prefab using the hit Info variable as the location. Also within the same IF statement we are going to play an audio source clip at the player position.
Back in Unity, we will drag and drop the sound clip and coin prefab into the requested slots on the player script. Save and hit play and watch as you can summon gold coins everywhere