Unity 3D Scripting Examples and Exercises: Worksheet Seven

You will need to refer back to previous examples and exercises and transfer the concepts and techniques introduced when carrying out this worksheet.

1.  Create a new Unity 3D Project called displayhud and import the Controller, Physics Materials and Scripts assets.

2.  Create a new folder called Textures inside the displayhud Assets project folder.

3.  Download the textures.zip from the module homepage and unzip the files.

4.  In Unity import the battery package: Assets – Import New Asset… and import all the textures you downloaded in step three above. Unity will store the textures in the Texture folder.

5.  Download the battery package from the module homepage.

6.  In Unity import the battery package: Assets – Import Package – Custom Package… this is a Prefab of a battery object with textures already applied. Unity will place it in the project’s Prefab folder.

7.  Create a new JavaScript file called BatteryCollect and place the following code on the script:

// static variable is accessable by other scripts i.e. its variable // scope is global

static var charge:int = 0;

var charge1tex:Texture2D;

var charge2tex:Texture2D;

var charge3tex:Texture2D;

var charge4tex:Texture2D;

var charge0tex:Texture2D;

// initialise GUI texture to false (don't display it)

function Start(){

guiTexture.enabled = false;

charge = 0;

}

/*

update function checks status of charge variable

which is increased via an external script

each time the player collides (collects) with a battery

*/

function Update () {

/*

first battery collected

assign the first texture image to guiTexture

and enable (display) texture

*/

if(charge == 1){

guiTexture.texture = charge1tex;

guiTexture.enabled = true;

}

// display subsequent textures to indicate power collected

else if(charge == 2){

guiTexture.texture = charge2tex;

}

else if(charge == 3){

guiTexture.texture = charge3tex;

}

else if(charge == 4){

guiTexture.texture = charge4tex;

}

else if(charge == 0){

guiTexture.texture = charge0tex;

}

}

8.  In Unity from the Textures folder select the battery_nocharge texture then from the Main Menu choose GameObject – Create Other – GUI Texture.

The empty battery texture should appear in the centre of the Game Window. You will need to adjust the x and y position to move the texture to the left bottom of the Game Window – see today’s lecture notes for the details. In the Hierarchy Window rename the battery_nocharge to HUD.

9.  Now select the BatteryCollect script , you should see the five public variable slots in the upper section of the Inspector Window.

10.  If you select the Textures folder you will see the five battery charge textures. Drag each texture from the Texture folder onto its respective variable slot in the script.

This makes an association between the script variables and the
textures to be referenced in the HUD script.

11.  Create a Plane, add a suitable material. Create a Direct Light and drag and
drop a First Person Controller into the scene.

12.  Add and position four battery Prefabs in the scene.

13.  Create a new JavaScript called PlayerCollision and add the following code:

function Start () {

}

function Update () {

}

function OnTriggerEnter(collisionInfo : Collider){

if(collisionInfo.gameObject.tag == "battery"){

BatteryCollect.charge++;

Destroy(collisionInfo.gameObject);

}

}

14.  Attach the BatteryCollect script to the HUD Game Object

15.  Run the scene. You should now be able to navigate the scene colliding with battery objects with the HUD being updated accordingly.

Exercises:

1.  Create some alternative artwork in Photoshop to replace the existing HUD textures. Use a suitable export format e.g. png 32.

2.  Create you own Unity scenario which features five coloured spheres for the player to collect. The collected spheres are displayed in a HUD with the associated sphere’s colour represented.

1