Day 68 — Implementing a Coin Distraction Ability Part 1
Hey and welcome!
So we have our movement for Darren in place along with our movement for the guards as well as the ability for them to spot Darren and cause a game over but we have no way currently in which the player can interact with these guards.
How we’re going to do that is by implementing a coin throwing mechanic for Darren that will distract the guards causing them to look at the coin and move towards it. Before we start getting fancy with this we’re going to start things off by giving the player the ability to right click anywhere in the scene and cause a coin to appear that the guards will head towards.
You can find an example of the above coin in your Prefabs folder. We’ll need to make some quick changes to this coin first though before we can use it so start by dragging this coin into the hierarchy and then unpack the prefab in the right click menu. You’ll notice that the coin is a child object of Coin so remove that as the child and delete the parent, you’ll know you’re deleting the correct one as the parent only has a transform component in it. Now delete all the extra components on there except for Mesh Renderer, Cylinder (Mesh Filter), Capsule Collider and the material itself.
Delete your old prefab and add this coin back in the Prefabs folder and you’ll be ready to start adding the following code to the Player script:
public GameObject coinPrefab;
public AudioClip coinSoundEffect;
private bool _coinThrown = false;void Update()
if (Input.GetMouseButtonDown(1) && _coinThrown == false)
Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo; if (Physics.Raycast(rayOrigin, out hitInfo))
_coinThrown = true;
Instantiate(coinPrefab, hitInfo.point, Quaternion.identity);
Since we’re going to be instantiating an object we start things off with a reference to the game object in coinPrefab. We also have an audio effect to play as well so we’re getting a reference to an AudioClip as well with coinSoundEffect. Then we have a bool here in order to introduce the logic to our code that makes it so that the player can only throw the one coin, otherwise it will be a bit too good and could cause problems in the game.
In the Update we’re adding a new if statement and you should recognise most of the code being used here. We’re using Input.GetMouseButtonDown in order to recognise a mouse click but this time we have 1 in here instead of 0 which means the code will run if the player right clicks in the scene. Though we have our _coinThrown bool in here which needs to also be false before the code will run.
We’re casting rays once again in order to recognise and get info on the point of impact that the player right clicks in the game. The second if statement here will run if the point of impact intersects with a valid collider in the scene which makes this the best place to change our _coinThrown value to true as we want to make sure the coin gets instantiated first.
Then we have our usual instantiate code using hitInfo.point as the location and we also have a new PlayClipAtPoint method that we use for the sound effect. This does exactly as you might guess and plays the audio clip at the position you specify which in this case is where the player right clicks.
Speaking of that, let’s go ahead and save the script and then add the audio clip and coin to the script component. You’ll find the audio clip in the SoundFX folder in the Audio folder.
With that done you should have the coin appearing in the game along with an audio clip when you right click.
I definitely waffled for longer than I intended here so I’ll be splitting this into two parts and in the next part I’ll cover how to make the guards look at and move towards the coin on the floor!