Adding Sound to the Damage Event

The last place we’re going to assign some player audio is in the player’s damage event. We provide feedback for damage with the flickering of our sprite and our particle effect, but we could use a little audio as well. Similarly to how we call our jump audio, we’ll call our damage audio from within a script. This will require some new code in both the PlayerController and PlayerStats scripts. Open the PlayerController script and make the changes shown in Listing 11.4.

Listing 11.4 Changes to the PlayerController Script

public AudioClip damageSound;public void PlayDamageAudio(){  this.audioSource.clip = damageSound;  this.audioSource.Play();}

We’ve added a new public AudioClip variable ...

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.