- Home /
Empty Health Bar, remove one life
Currently lm making a FPS game for iPhone. The player has 3 lives. How do I remove one life once the health bar runs empty? Here are scripts lm using:
NUMBER OF LIVES SCRIPT:
var health1 : Texture2D; //1 lives left var health2 : Texture2D; //2 lives left var health3 : Texture2D; //3 lives left static var LIVES = 3; function Update() { switch(LIVES) { case 1: guiTexture.texture = health1; break; case 2: guiTexture.texture = health2; break; case 3: guiTexture.texture = health3; break; case 0: //GAME OVER print("GAME OVER!"); break; }
}
FPS HEALTH SCRIPT:
var maximumHitPoints = 100.0; var hitPoints = 100.0; var bulletGUI : GUIText; var rocketGUI : DrawRockets; 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) { rocketGUI.UpdateRockets(rocketLauncher.ammoCount); /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]; }/ }
Please use code formatting so that your code is readable.
Answer by Preetthefire · Jul 09, 2012 at 05:14 PM
This will help you
var LifeControl;
function Start ()
{
LifeControl= GetComponent(NameOfTheScriptOfLifeControl);
}
now i suggest you to change this:
if (hitPoints < 0.0)
{
Die();
}
with
if (hitPoint < 0.0)
{
LifeControl.LIVES --;
if (LifeControl.Lives <= 0)
{
Die ();
}
}
i suggest you to insert some pickups to increase life percentage.
Answer by zmar0519 · Apr 09, 2011 at 10:40 AM
try explicitly stating a variable in the inspector for a lives gui script, and then change the static var to just var so that it can be accesed. then, you will have to modify your FPS Health script so that if the lives are greater than zero, don't die, but reset health and remove one life.
Your answer
Follow this Question
Related Questions
How do I set a max limit for my health value? 4 Answers
Head Shot = Instant kill for FPS enemies 1 Answer
Regain health on GUI 2 Answers
Problem with RPC life 0 Answers
Destroy 1 prefab object and changing the color/image 1 Answer