Coin Distraction (Part 1)

Objective: Set up the coin distraction logic and gameplay.

We previously set up our Player movement using the mouse left-click. We will now use the right-click to instantiate a coin along with an audio clip. This will be used later to alert the guards and move them to the coins position.

We create a public GameObject for the Coin and a public AudioClip to hold the coin sound. We then assign them in the Inspector.

We run a similar if input as we did for the left-click in the move post but instead of setting a destination for the Players movement, we instantiate the Coin and trigger the sound.

We also want to limit the Player to only having 1 coin. For this we create a private bool _hasCoin set to true. Next on the if input is right-click we add an and _hasCoin so that the logic within only runs when the Player has a coin. Finally inside the if Physics.Raycast meaning we have hit something we set _hasCoin to false when instantiating the coin and playing the sound.

In the next post, we will look at sending the guards to the area where the coin is.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store