• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wesley21spelde · Jan 22, 2013 at 11:37 AM · healthbar

fps script health tweak?

heu guys is it possible to use this fps script and tweak the health bar so i can have 15 GUITextures like this

 var health1 : Texture2D;
 var health2 : Texture2D;
 var health3 : Texture2D;
 var health4 : Texture2D;
 var health5 : Texture2D;
 var health6 : Texture2D;
 var health7 : Texture2D;
 var health8 : Texture2D;
 var health9 : Texture2D;
 var health10 : Texture2D;
 var health11 : Texture2D;
 var health12 : Texture2D;
 var health13 : Texture2D;
 var health14 : Texture2D;
 var health15 : Texture2D;
 var health16 : Texture2D;


and 1 question left can we make it so that it behaves like a halo 3of4 lifebar when not hit wait 8 recharge

 static var LIVES = 15;
 
 
 function Update () {
 var healthtexture=GameObject.Find("life bar 1");
 Healthcount=1;
 if(Healthcount<1){
 healthtexture.guiTexture.texture=health2;
 }
 if(Healthcount<2){
 healthtexture.guiTexture.texture=health3;
 }
 if(Healthcount<3){
 healthtexture.guiTexture.texture=health4;
 }
 if(Healthcount<4){
 healthtexture.guiTexture.texture=health5;
 }
 if(Healthcount<5){
 healthtexture.guiTexture.texture=health6;
 }
 if(Healthcount<6){
 healthtexture.guiTexture.texture=health7;
 }
 if(Healthcount<7){
 healthtexture.guiTexture.texture=health8;
 }
 if(Healthcount<8){
 healthtexture.guiTexture.texture=health9;
 }
 if(Healthcount<9){
 healthtexture.guiTexture.texture=health10;
 }
 if(Healthcount<10){
 healthtexture.guiTexture.texture=health11;
 }
 if(Healthcount<11){
 healthtexture.guiTexture.texture=health12;
 }
 if(Healthcount<12){
 healthtexture.guiTexture.texture=health13;
 }
 if(Healthcount<13){
 healthtexture.guiTexture.texture=health14;
 }
 if(Healthcount<14){
 healthtexture.guiTexture.texture=health15;
 }
 if(Healthcount<15){
 healthtexture.guiTexture.texture=health16;
 }
 }
 
 
 
 
 
 
 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;
         if (healthGUIWidth<15)
         Die();
     
         // 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];
             }*/
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FatWednesday · Jan 26, 2013 at 04:49 PM

You can have as many different health textures as you want, just do not store them like that. It makes it horrible to maintain and extend.

What you want to do is have a Texture2D array, with all of your textures added in the correct order, then you can simply use the value of HealthCount (modified by -1 if i understand your code) and use that as the index from the array to get the right texture.

that will cut down about 50 unneeded lines out of your code for a start, and make it far easier to maintain and extend.

as for the "recharge" yes its also very possible, I would do this by detecting when your health has reached that point, and setting a float value to say 8 (for a seconds) then if this value is greater than zero on each update, subtract Time.deltaTime from it. when this value reaches 0, your 8 seconds are up and you can recharge your health either instantly, or by triggering a smoother increase in the HealthCount value.

Also looking at your first update function there, it sets HealthCount to 1 at the start of Update() meaning only the second if statement there would ever run.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Splitscreen w/ different GUIs on each camera 2 Answers

Healthbar not always working, someone please help! 2 Answers

[help] enemy Health Bar decrease at the same time 2 Answers

Creating A GUI health bar 0 Answers

OnGUI() not showing anything 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges