HUD&Sound — UnityGameDebut

HUD & Sound— Episode #10

J3
KidsTronics
10 min readMar 7, 2018

--

Yes, this is the last one…here is the previous code. Our enemies are shooting randomly. Now we need a HUD and sounds effects, right?

The thrill of the very first moment I entered Unity has stayed with me till this very day.

The scope and content of this post is truly addressed and served the needs of the fine-tuning BR.Sniper — 3WaterTowerAssalt - The Game.

Here is the logic explained: There are 7 scripts: GameController.cs, Singleton.cs, HUD.cs, ShootHandler.cs, EnemyController.cs, Crosshair.cs, and Soldier.cs. See…

1-GameController object is general game organizer; it will often be called and should be unique; That’s why we use Singleton pattern;

2- We need a timer marker: while our game is running some object must count second by second — GameController.Count() will take care of this;

3- Some object must present the time to the player: HUD. mTxtTime property’s role;

4-While we are shooting some object must manage the damage logic: ShootHandler’s role; while we hit enemy this damage are not the same; sometimes we hit enemy’s arm, another we hit his head; in order to simulate this, the code will set a random damage by using Random.Range() class and extend it to make an enemy randomly active too; see ShootHandler.ShootEvent();

5-So far, so good! now the same thing happen when it’s time to present the enemy’s health; he probably will be hit too and for this logic we have GameController. mHealth property; HUD. mTxtHealth will present to the player what is his health status;

6-Now some object must control when the game is over: GameController. mGameOver property’s role; for this it must raise an event at the moment the health of the player reaches zero or when the playing time is over. GameController.GameOver()’s role; it will set a flag to true (mGameOver) and will raise GameOver(); another role of this object is to offer information to other objects (see public getter properties: IsGameOver, IsWon, Health, Time); The message (YOU WON/LOOSE) will be presented in a painel, initially disabled and controlled by the HUD object which will be a listener of the gameOver event raised by the GameController object;

7-The remaining tasks are: how to know the configuration in Unity of the changes of animations and this is explained in the step of number #08; how to insert sound effects (step #29); how to raise events since they occur in the main thread. For this we use the delegate in C # (public event EventHandler GameOverEvent from GameController), which transmits to other threads the event. This is a technical question of C#. Some language makes this easier, others don’t…it’s enough…

My apologies for including that long explanation to just one post…I sincerely hope that people will start using Unity and understand what they are doing :)

Let’s start the code by Singleton, remember?

Step #01 — A Singleton is a script that cannot be an Instanced or Duplicated;

Head over Unify Community Wiki, copy and paste the 1) Singleton.cs code to a new C# script with the same name; save it;

Step #02—Create a gameObject, rename it to 2) GameController, Create C# Script, name it GameController.cs and type:

First thing: set a variable to store the player's current health; On GameController get rid of Start() and Update():

And add the set damage method to GameController:

To retrieve the current health include this property to GameController:

Create > C# Script, name it HUD.cs; Enter inside this script and add:

And on HUD.Update()

Now we need a ShootEvent() to apply the damage (by calling SetDamage of the GameController), but first let’s present an interface named HUD — Heads Up Display;

Step #03 — Let’s set up a HUD — Heads Up Display:

Go to Explorer, rename Canvas to HUD, choose HUD.cs and drag n’ drop to the objectGame just created;

HUD

Step #04 — Add UI text by selecting HUD, right-click it and create UI > Text, name it TxtHealth

Step #05 — Set TxtHealth configuration like this:

Rect Transform: Pos x=247; Pos y=-92; Pos Z=0

Widht=100; Height=30

Text = 100

Font: Arial; style=bold; size=24

Aligment=Left Center

Color = White

Step #06 — Create a folder under Assets and name it Images; import these images to this directory: heart.psd and time.png from:

Google Drive

In the Images folder select the 1) heart.psd image, 2) choose Sprite and click 3) apply; now the 4) image background is transparent;

Now Create a UI > Image (ImgHealth) and drag the heart.psd to the Image Source slot;

make the final adjustments using w,e & r keys

Step #07 — Now we will create two objects: TxtTime (UI >Text) and ImgTime (UI >Image); Repeat the same procedures of the steps 4 and 5; Here is the HUD result:

So far, so good!

Step #08 — Now let us work with animation; we will use animations gameObject to call a custom function: 1) Select ToonSoldier; 2) click f while hovering over Scene view; the soldier will be selected; 3) Click the shoot animation from ToonSoldier_demo > animation; 4) Click Edit;

Now With the assault_combat_shoot selected, 1) click Events, 2) Add Event, 3) Function = ShootEvent, 4) hover to the appropriate point, 5) select ToonSoldier and a window pops up; 6) click apply and you’re done!

Now when this animation runs at this point it will trigger a function named ShootHandler.ShootEvent();

Step #09— Return to the Assets > Script folder, Create a new C#Script, name it ShootHandler.cs, attach it to ToonSoldier_demo_1 by dragging n’ drop it; Than after get rid of Start() and Update() type this method:

Now we need to add the time ticks, that represents a non-negative integer which specifies the elapsed time between two events;

First, we’re going to extend the UI interface;

Step #10— Do this: 1) select HUD and 2) drag-drop TxtHealth to MTextHealth property to attach it to the script;

Step #11 — Go to the GameController.cs and add:

And right below we need getter property:

Step #12 — Go to HUD.Update() and assign the available time to the text:

Step #13 — On the Unity app, select HUD.cs and 2) drag n’ TxtTime to the appropriate slot:

Step #14 — Now comes the counting down logic: open GameController.cs and type a new property:

Step #15 — In the GameController.Start():

And right beneath:

But first, check if the game is over:

Step #16 — Set the logic of the damage only if the game isn’t over yet and set the game over if the player is dead:

Step #17 — Make a getter bool property:

Step #18—Make sure the enemy won’t appear anymore when the game is over; go to EnemyController.Update():

Save everything.

Good!

Now when the time is over, the enemies remain in the idle position.

Now the End Screen!

Step #19— Now we need a painel (PnlGameOver) to display if the player won or loose;

Create a painel by right click the HUD on Explorer UI > painel; 1) Center it (center+middle); 2) Width = 200, Height=60; and finally 3 and 4) Color=light dark;

Step #20— Now right click the painel and add a Text and name it TxtGameOver; and configure it: 1) center it; 2) Width=200, Height = 70; and 3) Text = YOU WIN; 4) Font Style & Size= Arial, 25; and 5) Align=both center; 6) Color = White

Now deselect the panel (this will be enabled via script):

Step #21— In order to access this UI object in the HUD.cs add 2 member variables (click saveAll); drag it to the appropriate slots;

Drag n’ drop these panel variables

Step #22 — Go to the GameController.cs and type this getter property:

Step #23 — Now when the game is over I will fire an event (a EventHandler a C# delegate) And add a method that causes this event and it is bound in another class; Go to GameController.cs and type (type using System; at the head):

Step #24 — And inside GameController.cs add a method that causes this event and it is bound in another class:

Step #25 — Call this method whenever the mGameOver set to true;

Step #26 — In the HUD.cs bind the delegate in the Start();

Step #27note: when typing += a pop up appears; click the tab to complete the code;

Step #28 — Switch to the Crosshair.cs and also bind the event there; (click saveAll)

Step #29 — Now Sounding it! Go to Asset Store, 1) Download Button, 2) Filter by post, 3) Select Post Apocalypse Guns Demo, 4) click import two times and 5) a directory will be created;

SOUND EARTH GAME AUDIO — Post-Apocalypse Guns

Step #30 — Select 1) HUD.Crosshair gameObject 2) click Add Components, 3) choose Audio Source and drag n’ drop AutoGun_1p_01 sound to AudioClip slot and 4) deselect Play on Awake;

In the code these settings are related to sound effect (check out the code below):

Step #31 — Go to ShootHandler.cs and add these methods to ToonSoldier object so that we can hear the rifles of the enemies; Add Audio Source, assign an Audio Clip; To apply to another soldier just click Apply button on Inspector.

— — — — — — — —

Now all the code. I will present the complete code now:

Step #32— This is GameController:

GameController
GameController

Step #33 — This is ShootHandler:

ShootHandler
ShootHandler

Step #34— Now Soldier.cs:

Soldier
Soldier

Step #35— Open HUD.cs and type:

HUD
HUD

Step #36 — Crosshair.cs modifications:

Crosshair
Crosshair

Step #37 —EnemyController.cs

EnemyController
EnemyController

You can make a game work now, but artists make it look good!

This is your turn!

Check out the complete code from my Github. Thanks!

Download 3WaterGame_Unity Complete!

# 0 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10

GitHub Repo

References & Credits

Making a Game with Unity (1–10) : Basics by Jayanam

--

--

KidsTronics
KidsTronics

Published in KidsTronics

Get children evolving with Arduino, Rpi & PIC using MIT App Inventor 2 & Lego.

J3
J3

Written by J3

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️