Game Development Reference
In-Depth Information
5. In the final few functions, shown next, LateUpdate calls the UpdateGUI function,
which manipulates the length of the texture image stored in the healthGUI variable
game object. All this means that the GUI is updated as one of the last tasks for
each frame so that nothing that occurred in the frame is missed. Additionally, the
PlayStepSounds.js script, which was called back during the Awake function, plays
sounds whenever the player is walking.
function LateUpdate () {
// Update gui every frame
// We do this in late update to make sure machine guns etc.
// were already executed
UpdateGUI();
}
function PlayStepSounds () {
var controller : CharacterController = GetComponent(CharacterController
);
while (true) {
if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
audio.Play();
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
function UpdateGUI () {
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + r
healthGUIWidth* healthFraction;
}
6. Once you've finished entering all the code, select your First Person Controller object
and add the FPSPlayer.js script to it by dragging the script to the FPS controller's
Inspector view.
7. The script will ask for a GUI Texture object so the player can track their health. This
will be the player's health bar. Go to the Create menu in the Hierarchy view and select
GUI Texture from the list. This will put a Unity logo in the middle of your game window.
8. Rename the GUI Texture object HealthGUI in the Hierarchy view and then look at
its Inspector options. Under the Texture variable is the Unity logo. Change this to a
health bar texture of your own creation or the healthBarStandIn.tiff file found in
the online resources version of the project. The texture image found in the project
files is a simple 32 pixel n 32 pixel red square, so any file you create yourself does not
need to be any bigger or more complex for now.
Search WWH ::




Custom Search