Creating a Distraction System in Unity

We need to create a way for the Player to distract the guards. To get past them, we can allow our Player to throw a coin and redirect the NavMeshAgent components of the guards towards it.In our Update method we can run a method named ClickToThrow, this…

We need to create a way for the Player to distract the guards. To get past them, we can allow our Player to throw a coin and redirect the NavMeshAgent components of the guards towards it.

In our Update method we can run a method named ClickToThrow, this will detect whether the Right Mouse Button has been pressed. If so, we can start a Coroutine to run through the sequence of throwing the coin. We also only want the Player to have one coin, so we can create a bool.

In the Coroutine, much like our Left Click to move, we want to use a Raycast to detect where the mouse is, so as to have a position to Instantiate our coin.

Before we Instantiate the coin, we can set an Animation Trigger to switch states to our Throw animation. This can be accessed from Any State and then depending on the Walk bool will return to the appropriate state.

We then want to get the length of this Animation clip and use it as a time variable for a WaitForSeconds, before we Instantiate our coin prefab.

Additionally, I’m also setting the bool for the NavMeshAgent to be stopped while the throw animation occurs, this prevents any unwanted shuffling to the destination while the walk animation isn’t occurring.

For the Sound Effect, I’m using Wwise, so I need to Post the Wwise Event, and for this sound, I want it to relate to where the coin is thrown in relation to the camera, therefore I’m Posting it on the coin.

Lastly, I need to let the guards know about the coin.

To distract the guards, we can create an Array that stores all game objects with the appropriate tag. Then, using a foreach loop we can access each guard’s AI Script, and call a function, passing in the position of the coin.

In our GuardAI script, our public method receives the coin position, stores it in a local private variable, and sets an alerted bool to true.

From here, we can adjust our existing waypoint system. First, we need to place the existing WayPointMovement method inside an if statement checking the alerted bool.

If alerted is true, we can instead SetDestination to the coin position. Then we can check our distance from the destination, before running our IdleTime Coroutine.

In our Coroutine we can again store everything in an if statement, as we want to increase the WaitForSeconds time when the guard’s are looking at the coin. From here, we return the alerted bool to false and continue getting the next waypoint.

With the guards distracted, we should probably let the User know they can actually throw a coin. Luckily we have a voiceover to tell Darren what to do. We can set up another box collider trigger that will run a modular Voiceover script.

In our Script we simply create a handle to the Wwise Event then Post it on the main camera when the Player enters the trigger.

Lastly, we want to make the audio slightly more immersive. Realistically, we could probably get away with the same sound effect every time, as we can’t throw multiple coins one after another.

Either way, I’ve used Wwise to not only randomize the pitch, but to create different panning paths for the coin bouncing.

To do so, we can access the Positioning tab and create Automation. We can then create multiple paths that are randomly selected for our panning. This can be very noticeable in first person, but even with our game, it creates some nice variation.


Creating a Distraction System in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Calum Slee | Sciencx (2024-03-28T21:16:49+00:00) » Creating a Distraction System in Unity. Retrieved from https://www.scien.cx/2021/08/26/creating-a-distraction-system-in-unity/.
MLA
" » Creating a Distraction System in Unity." Calum Slee | Sciencx - Thursday August 26, 2021, https://www.scien.cx/2021/08/26/creating-a-distraction-system-in-unity/
HARVARD
Calum Slee | Sciencx Thursday August 26, 2021 » Creating a Distraction System in Unity., viewed 2024-03-28T21:16:49+00:00,<https://www.scien.cx/2021/08/26/creating-a-distraction-system-in-unity/>
VANCOUVER
Calum Slee | Sciencx - » Creating a Distraction System in Unity. [Internet]. [Accessed 2024-03-28T21:16:49+00:00]. Available from: https://www.scien.cx/2021/08/26/creating-a-distraction-system-in-unity/
CHICAGO
" » Creating a Distraction System in Unity." Calum Slee | Sciencx - Accessed 2024-03-28T21:16:49+00:00. https://www.scien.cx/2021/08/26/creating-a-distraction-system-in-unity/
IEEE
" » Creating a Distraction System in Unity." Calum Slee | Sciencx [Online]. Available: https://www.scien.cx/2021/08/26/creating-a-distraction-system-in-unity/. [Accessed: 2024-03-28T21:16:49+00:00]
rf:citation
» Creating a Distraction System in Unity | Calum Slee | Sciencx | https://www.scien.cx/2021/08/26/creating-a-distraction-system-in-unity/ | 2024-03-28T21:16:49+00:00
https://github.com/addpipe/simple-recorderjs-demo