Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by johnniks · Nov 16, 2015 at 08:41 PM · healthbarlocalscalecolor.lerphealth

How do I make my healthbar change color depending on damage?

Hello, I'm really stuck on my healthbar and could really use some help! I'm trying to make my healthbar go from green -> yellow -> red depending on how much damage the player has gotten. But my current attempt at a color change code in my setHealth() {...} just makes the healthbar vanish :/

Any ideas on what I can do to make this work? Because I'm really lost :( THANKS!

PS. I've commented out the color code part on purpose!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class EnemyHealth : MonoBehaviour {
     
     public float hitPoints = 100f;
     public float currentHitPoints;
     public GameObject destroyFX;
     public Image healthbar;
 
     public Color32 startColor;
     public Color32 middleColor;
     public Color32 endColor;
 
     void Start () {
         currentHitPoints = hitPoints;
         setHealth();
     }
     
     void TakeDamage(float amt) { 
         currentHitPoints -= amt;
 
 
         if (currentHitPoints <=0) {
             currentHitPoints = 0;
             Die();
         }
 
         setHealth();
 
     }
     
     void Die() {
         if(gameObject.tag == "Enemy") {
             Instantiate(destroyFX, this.transform.position, this.transform.rotation); 
             Destroy (gameObject);
         }
     }
 
     public void setHealth() {
         float enemyHealthCalc = currentHitPoints / hitPoints; 
 
         healthbar.transform.localScale = new Vector3(enemyHealthCalc, healthbar.transform.localScale.y, healthbar.transform.localScale.z);
 
         /*
         if (enemyHealthCalc < 0.1f) {
             healthbar.color = Color.Lerp(endColor, middleColor, enemyHealthCalc * 2);
         }
         else {
             healthbar.color = Color.Lerp(middleColor, startColor, (enemyHealthCalc - 0.5f) * 2);
         }
         */
     }
 
 }
 
Comment
Add comment · Show 2
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
avatar image say_forever · Nov 17, 2015 at 07:31 AM 0
Share

you are almost right.

 Color Lerp(Color a, Color b, float t); 

using Color.Lerp need notice that "t" is clamped between 0 and 1

avatar image johnniks say_forever · Nov 17, 2015 at 06:54 PM 0
Share

How would one clamped "t" between 0 and 1? $$anonymous$$athf.clamp or something?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by say_forever · Nov 18, 2015 at 09:31 AM

you can write in Update() like

 private float t = 0;
 void Update()
     {
         Color c = Color.Lerp(Color.white, Color.black, t);
         t = t + 0.01f;
     }
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.

Follow this Question

Answers Answers and Comments

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Kill Count and Health Bar Scripts 0 Answers

My health bars image.fillAmount doesnt change. 4 Answers

Bullet Damage Player's Healthbar,Bullet Damage Other Player's Health 2 Answers

Unity Multiplayer: when i hit another player all players health bars go down 0 Answers

Hey Guys I want my player to lose one Heartcontainer if he collides with the enemy 0 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