Journal 71 — “The Great Fleece” in Unity, Add Coin Distraction

Chris Nielsen
4 min readAug 17, 2021

--

Objective: Implement a coin distraction feature in the game

In “The Great Fleece”, we completed the rest of the security guard AI animations and walking with waypoints. Now we want to add a coin distraction feature to help Darren sneak past the guards.

To start, let’s update the Player C# script.

We can add a reference to the coin game object and sound effect, and we can use Input. GetMouseButtonDown(1).

Before using the coin prefab, let’s check the scale, position, and components. I changed the scale to 0.25, 0.01, 0.25 in order to better see it in game. We can delete the script, audio source, and animator as those aren’t needed.

Now let’s see this in action.

Let’s add a restriction that only one coin can be tossed. We can simply add a bool to the update method that allows for the right mouse click once.

Guard AI Update

Now that we can toss a coin, let’s update the Guard AI so they will all change their destination to the coin spot. In the Player script, here’s where we can start.

In the GuardAI script, we need to update the waypoint movement with a public bool so that if the coin is tossed, the Player script updates the state, and the waypoint movement will be skipped. We can add an else function to change from the walk animation to idle when the guards are within a certain distance of the coin.

Here this is in action. You may need to adjust the distance from the coin in the GuardAI script, and also each guard NavMeshAgent stopping distance to finetune their walk to idle movements.

Animate Throw

Now let’s add an animation for when Darren tosses a coin. We previously added a “Throw” animation to Darren’s Animator.

Let’s add a “Throw” trigger to show the throw animation.

In code, all we need to add is SetTrigger(“Throw”) as shown below.

Now let’s see this in action.

Voiceover Trigger

Now let’s trigger a voiceover that explains how Darren can toss a coin to distract the guards. In “The Great Fleece” assets, there are voiceover triggers along the floor and audio clips to reference. Let’s first create a voiceover trigger C# script. Then we can add the script to the trigger game objects.

We also need to update the three triggers with colliders set to trigger and a Rigidbody with gravity turned off.

In the voiceover script, just need a reference to the audio clips, and a void ontriggerenter function to check for the “Player” tag, and play an audio clip. Be sure to drag the audio clip into the Inspector.

Thank you for your time!

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

No responses yet