Game Development Reference
In-Depth Information
Adding Other Gameplay Elements
In this last section, you will use a few more short scripts to add some other game elements
to your Unity scene.
Adding Ammo Counters
You will now add two ammo counters to the scene, one for bullets in the current clip and
one for bullets left in reserve.
1. If it is not already open, open your Unity project and scene.
2. Create two new GUI Text (not GUI Texture) objects with the Hierarchy view's Create
menu: one called BulletGUI and another called ClipsGUI . GUI Text objects work much
like the GUI Texture you used earlier, except they contain text options in their
Inspector window in addition to options for placement.
3. Move each of these objects so they are in the bottom-right corner of the screen in line
with the healthGUI object you placed earlier in the chapter. Place ClipsGUI so it is fur-
ther right than BulletGUI .
4. Change the font size of each to 48 . Create a new folder in your Project view called
Fonts and copy a zombie-game appropriate font from your computer into this folder.
Place this font so that it applies to both of your new GUI Text objects.
5. Create a new script called AmmoCount.js and add it to BulletGUI .
6. Type the following into AmmoCount.js :
function Update () {
guiText.text = “Ammo: “+GameObject.Find(“HandGun”).
GetComponent(Handgun).bulletsLeft;
}
This script checks the value of the bulletsLeft variable in the HandGun.js script
every frame and makes it the text of the GUI Text object. Any elements of guiText.
text that you place in quotes will be printed statically, so the script will display
“Ammo:” before the dynamic ammo number.
7. Create another new script called ClipCount.js and add it to ClipsGUI . Type the follow-
ing into ClipCount.js :
function Update () {
guiText.text = “/ “+GameObject.Find(“HandGun”).GetComponent(Handgun).
extraBullets;
}
As you can see, this script is very similar to AmmoCount.js , except it calls the extra-
Bullets variable instead of bulletsLeft . Also, the static portion of this text is / , so
Search WWH ::




Custom Search