0% found this document useful (0 votes)
7 views

2D Unity Lab 7 (Audio Shooting)

Uploaded by

hamdyaboaouf24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

2D Unity Lab 7 (Audio Shooting)

Uploaded by

hamdyaboaouf24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Player Attack|Coin

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!

• In the Update() function

• Create a new function that will handle shooting


Exercise #27 – Player Shooting Mechanism
(Cont.)
• Go back to Unity and give the FirePoint and Bullet variables their
correct game objects by dragging and dropping into the text boxes
• (Also don’t forget to give the Return KeyCode its correct button)

• 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!

• Particle Effects! - Unity 2D Platformer Tutorial - Part 5


https://www.youtube.com/watch?v=vwUahWrY9Jg

• Shooting Projectiles & Camera Control - Unity 2D Platformer Tutorial - Part 8


https://www.youtube.com/watch?v=8aVZuL9ocrk
Exercise #28 – Stomp on an enemy’s head
• You will need to have multiple 2Dcolliders on the enemy GameObject, and for the
stomping mechanism, the collider must have IsTrigger checked

• 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 a variable of game object and name it enm (enemy)

• 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”

• We need the Audio Manager to have 3 things:


• Two Audio source to hold the sound effects and the Background music
• Range of High and Low pitches
• An instance of Audio Manager itself for other scripts to call it.
Exercise #29 - Audio Manager (Cont.)
• Variables needed:

• 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

• Create a RandomizeSfx () function


Exercise #29 - Audio Manager (Cont.)
• Now, to initialize the two AudioSource variables in the Audio
Manager script, we need to create two Audio Source
components in the Audio Manager game object and give them
their suitable audio clips to be run afterwards. For the first
Audio source , we need to check the Play On Awake and Loop
check boxes since that Audio Source holds our Background
music that will be looping the whole time. While the second
Audio Source, we leave it as it is, just uncheck the Play On
Awake check box.
• We, then, drag and drop those two Audio Sources into their
corresponding variables in the Audio Manager script.
Exercise #30 – Player Sound Effects
• If you need to add sound effect when your player jumps:
• Go to the “Controller” script and add two AudioClip variables, to hold two different sounds
that will be randomly played.

• 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.

Then, inside the TakeDamage() function,


where the we check on the health and
lives of our player, call the PlaySingle()
function and pass to it your AudioClip
variable.
Also, we need to stop the BackGround
music playing since the game is over.
Useful References
• Sound Effects - Unity 2D Platformer Tutorial - Part 10. URL retrieved from:
https://www.youtube.com/watch?v=0QWXZDcJEIw
• Unity 2D Game Development 18 : Audio Clips. URL retrieved from:
https://www.youtube.com/watch?v=FezUGIk-4C4
• Creating 2D Games in Unity 4.5 #32 – Sounds. URL retrieved from:
https://www.youtube.com/watch?v=pBxG57bjDZs
• Audio and Sound Manager. URL retrieved from:
https://unity3d.com/learn/tutorials/projects/2d-roguelike/audio
• Pickups & Points - Unity 2D Platformer Tutorial - Part 6. URL Retrieved from:
https://www.youtube.com/watch?v=J4dkxdbe8FI
• How To Add A Health Bar & Health Pickups - Unity 2D Platformer Tutorial -
Part 26. URL Retrieved from: https://www.youtube.com/watch?v=-a5JIZih0TM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy