- Home /
Health bar using switch statement
I have the health system already set up through the fps tutorial (FPSPlayer Script)I wanna make a health bar that switches the image every 10%. I do NOT wanna scale one image. I have 10 textures total and this is my switch script
var health10 : Texture2D;
var health20 : Texture2D;
var health30 : Texture2D;
var health40 : Texture2D;
var health50 : Texture2D;
var health60 : Texture2D;
var health70 : Texture2D;
var health80 : Texture2D;
var health90 : Texture2D;
var health100 : Texture2D;
static var HEALTH = 10;
function Update ()
{
switch(HEALTH)
{
case 10:
guiTexture.texture = health100;
break;
case 9:
guiTexture.texture = health90;
break;
case 8:
guiTexture.texture = health80;
break;
case 7:
guiTexture.texture = health70;
break;
case 6:
guiTexture.texture = health60;
break;
case 5:
guiTexture.texture = health50;
break;
case 4:
guiTexture.texture = health40;
break;
case 3:
guiTexture.texture = health30;
break;
case 2:
guiTexture.texture = health20;
break;
case 1:
guiTexture.texture = health10;
break;
case 0:
//game over
break;
}
}
And I wanna connect it to the FPSPlayer script which is this:
static var maximumHitPoints = 100.0;
static var hitPoints = 100.0;
var bulletGUI : GUIText;
var rocketGUI : GUITexture;
var healthGUI : GUITexture;
var walkSounds : AudioClip[];
var painLittle : AudioClip;
var painBig : AudioClip;
var die : AudioClip;
var audioStepLength = 0.3;
private var machineGun : MachineGun;
private var rocketLauncher : RocketLauncher;
private var healthGUIWidth = 0.0;
private var gotHitTimer = -1.0;
var rocketTextures : Texture[];
function Awake () {
machineGun = GetComponentInChildren(MachineGun);
rocketLauncher = GetComponentInChildren(RocketLauncher);
PlayStepSounds();
healthGUIWidth = healthGUI.pixelInset.width;
}
function ApplyDamage (damage : float) {
if (hitPoints < 0.0)
return;
// Apply damage
hitPoints -= damage;
// Play pain sound when getting hit - but don't play so often
if (Time.time > gotHitTimer && painBig && painLittle) {
// Play a big pain sound
if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
audio.PlayOneShot(painBig, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
} else {
// Play a small pain sound
audio.PlayOneShot(painLittle, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
}
}
// Are we dead?
if (hitPoints < 0.0)
Die();
}
function Die () {
if (die)
AudioSource.PlayClipAtPoint(die, transform.position);
// Disable all script behaviours (Essentially deactivating player control)
var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms) {
var p : MonoBehaviour = b as MonoBehaviour;
if (p)
p.enabled = false;
}
LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);
}
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 () {
// Update health gui
// The health gui is rendered using a overlay texture which is scaled down based on health
// - Calculate fraction of how much health we have left (0...1)
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
// Update machine gun gui
// Machine gun gui is simply drawn with a bullet counter text
if (machineGun) {
bulletGUI.text = machineGun.GetBulletsLeft().ToString();
}
// Update rocket gui
// This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
// to the RocketTextures property.
if (rocketLauncher) {
if (rocketTextures.Length == 0) {
Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property.");
} else {
rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
}
}
}
I somewhat know u can calculate the percentage by this
var healthPercent : float = hitPoints/maximumHitPoints;
although where to put that, and how to apply that to the switch statement beats me... Can someone help me please??
Answer by jahroy · Jan 13, 2012 at 09:11 PM
The first thing you should do is replace your ten variables with an array of ten elements like this:
var textureArray : Texture [] = new Texture [10];
You can assign the elements of the array in the Inspector.
Then you can replace your switch statement with this:
guiTexture.texture = textureArray [ HEALTH ];
That sure is a lot of code to read...
I'll admit I haven't read it, but I don't see why you need your health logic to be in a separate script. (of course it would be easy to use separate scripts... read this OR this)
I would add the array of textures from above to your FPS script.
Then you can calculate health just like you mentioned and use it to choose an element from your array of textures.
You can calculate your health percentage, then divide it by 10, then convert it to an int to make it work like you want your HEALTH variable to work.
Your answer
Follow this Question
Related Questions
how to swap a mesh using an array 1 Answer
C# Array Issue 2 Answers
Storing weapon types and changing between them 1 Answer
Switching Weapons (C#) 2 Answers
Issues with random music player 1 Answer