Objective: Implement sound effects and animations.
As it stands, the player can now throw a coin and the guards will stop their patrol and head to the coin, causing an excellent distraction. Now I’m going to show you how to implement sound effects as well as your animation.
Let’s begin with the sound effect. If you have a sound effect ready you can easily add it to your player. Open the player script and add a variable to store the sound effect.
Now in the inspector view you can drag and drop your sound effect into the audio clip.
To play this audio clip simply call it where you want the sound to play I.E. the player’s position so it will be heard. I would call this audio clip when the coin in instantiated.
Moving on to the animation. If you already have you animation created, this tutorial will show you how to implement the animation. Drag you throw animation into the animation controller and make a transition from any state to the throw animation.
Using any state means the player’s throw animation will play weather the player is idle or walking. We want the animation to be able to transition to both the idle and walk animation as well so make a transition.
On the transition to from throw to walk add the condition ‘walk = false’ and do the opposite to for the throw to walk. The transition for the any state to throw needs a new parameter. Create a trigger parameter and name it Throw.
We should already have a handle on the player script for the animator so all we need to do now is call set off the throw trigger. To do this when we instantiate the coin we will call the throw trigger.
Now the player will trigger the throw animation when they right click to ‘throw’ the coin. Don’t be afraid to experiment with your animations and I’ll see you in the next tutorial!