Adding in sound effects/music
Another thing that we can do to give the project a little more polish is add in sound effects and background music. Let's do that now. Perform the following steps:
- Select your enemy prefab, and add an Audio Source component to it by selecting Component | Audio | Audio Source.
- From there, go into the Inspector and uncheck the Play On Awake property. Otherwise, if there is something attached to AudioClip it will play that sound automatically.An audio source lets the audio listener attached to the Main Camera object know that this is an object that can play sounds.
- After this, let's go into our
EnemyBehaviour script! As usual, we'll need to add in a new variable for us to use to play whenever we're hit: