2D Unity Lab 7 (Audio Shooting)
2D Unity Lab 7 (Audio Shooting)
Collection|Audio Management
__________________________________________
7
Exercise #27 – Player Shooting Mechanism
• Choose a suitable particle, weapon or bullet that you want your
character to fling at the enemies to kill them
• Resize it so that it’s not too big
• Add a rigidbody2D and change the gravity scale to 0
• Add a collider2D and set IsTrigger to be true
• Add a script and name it BulletController. We’ll come
back to it later
• Turn the bullet into a prefab
Exercise #27 – Player Shooting Mechanism
(Cont.)
• Create an Empty GameObject and name it FirePoint. This will be the
point from which you fire bullets/particles/etc
• Change the object into a red sphere to be able to see it. Place it
where you want on the player and make it a child of the player
Exercise #27 – Player Shooting Mechanism
(Cont.)
• Go to the Player’s controller script to give him/her the ability to fire, same as you
game them the ability to walk, jump, etc!
• Make sure you drag the PREFAB of the spiky bullet! This will allow the player
to shoot as many times as they want
Exercise #27 – Player Shooting Mechanism
(Cont.)
• Now go back to the BulletController script and write the following:
Exercise #27 – Player Shooting Mechanism
(Cont.)
• Next are the Update and OnTriggerEnter2D functions
Exercise #27 – Player Shooting Mechanism
(Cont.)
• TIP: If you want to create a particle effect so that your enemy
explodes to bits the moment your bullet hits them, check out these
2 tutorials!
• In order to have two different colliders act in different ways, you must use two
separate game objects. The way to get around this is to put the one collider on a
game object, then add the second on a child of the game object
• To do this:
• Create an empty game Object and make it a child of your enemy , add a circle collider to the
empty object and also enable IsTrigger
• Now the child object will need to have its own Monobehaviour script attached to recognize
the new collider
Exercise #28 – Stomp on an enemy’s head
(Cont.)
Exercise #28 – Stomp on an enemy’s head
(Cont.)
• Add a script called KillEnemy to the child game object. This script executes when the player’s
collider collides with the enemy’s:
• Create an OnTrigger function and write a statement where if the collider that collides with the
child game object belongs to the player, the enemy game object is destroyed
Exercise #29 - Audio Manager
• Create a script that contains everything needed to control the sounds
of your game, either the background music or the sound effects on
different events. Call it AudioManager and attach it to a new empty
gameObject called “Audio Manager”
• Note1: when you declare a ‘static’ variable, it means the variable belongs to the class itself. It’s
therefore shared by ALL object instances made of the class, and only one of it exists across all of
them.
• Note2: using the keyword static allows you to access AudioManager from other scripts and
objects without having to pass any references. It allows you to access AudioManager directly
through the class name without needing to create an instance/object of the class.
Exercise #29 - Audio Manager (Cont.)
• In the Start() function, we name it Awake() so that it is called once the game is played. Here, we
just create an instance of AudioManager and make sure it is never destroyed when reloading new
scenes
Exercise #29 - Audio Manager (Cont.)
• Create a PlaySingle() function
• Then, inside the Jump() function, where the Jumping animation take place, call the
RandomizeSfx() function and pass to it your two AudioClip variables.
• Following the same technique, you can add sound effects to every action / event taking place
in your game.
Exercise #31 – Enemy Hit Sound Effects
• You will need to add sound effect when an enemy hits your player, to do so:
• Go to the “Enemy Controller” script and add two AudioClip variables, to hold two different
sounds that will be randomly played.
• Then, inside the OnTriggerEnter2D() function, where the damage takes effect when an enemy
hits a player, call the RandomizeSfx() function and pass to it your two AudioClip variables.
Exercise #32 – Collect Coins/Items
• First, create coins:
• Choose suitable coin sprites and make them varied – gold, silver and bronze coins. Each will have
a different value when the player picks them up
• Add a Circle Collider 2D to each coin
• Check the “Is Trigger” property
• Create a new script and name it CoinPickup
Exercise #32 – Collect Coins/Items (Cont.)
• The script executes when the player’s collider collides with the coin’s collider due
to the checked IsTrigger property
• The Destroy function makes the coin object disappear off the screen, giving the
illusion that the player has picked it up
• The public variable also allows to set the value of the coin . Set the values for the
coins as follows:
• coinBronze: Set the “Coin Value” property to 1.
• coinSilver: Set the “Coin Value” property to 5.
• coinGold: Set the “Coin Value” property to 10.
Exercise #32 – Collect Coins/Items (Cont.)
• You have the player collecting coins, but the total number of coins are not stored
yet
• Create a new variable and a function to store that value in PlayerStats. Something
like this:
Exercise #33 – Game Over Sound
• You will need to add sound effect when your game is over, to do so:
• Go to the “PlayerStats” script and add one AudioClip variable, to hold one
single sound that will be on Game Over.