Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 colourcrisis · Sep 16, 2018 at 02:42 PM · colorlerpmathhealthbarcolor.lerp

Healthbar color lerp between green, yellow, and red

Hi everyone, I'm trying to build a health bar that lerps between three colors based on the halfway point... I used two lerps that runs based on if it has passed the halfway point. But it isn't working as it is looking at the whole health bar rather than the halfway points. Any help would be appreciated.

  if ((HitPoints / TotalHitPoints) >= 0.5f)
         {
             healthBar.BarColor = Color.Lerp(Color.yellow, Color.green, (float)((float)healthBar.Value / (float)healthBar.MaxValue));
         }
         else if ((HitPoints / TotalHitPoints) < 0.5f)
         {
             healthBar.BarColor = Color.Lerp(Color.red, Color.yellow, (float)((float)healthBar.Value / (float)healthBar.MaxValue));
         }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by eses · Sep 16, 2018 at 03:49 PM

Hi @colourcrisis.

You don't necessarily have to use Lerps between pairs of colors - you could use one Gradient instead. That way you can easily lerp between 0-1 while showing any color from gradient.

Then do a call to method that can be something like this:

 Color ColorFromGradient (float value)  // float between 0-1
 {
     return gradient.Evaluate(value);
 }


You can define your gradient to have same colors you have now, either from code or by setting the colors of the gradient from inspector. The simplest way is to use inspector, just create public field and change the colors:

 public Gradient gradient;

Comment
Add comment · Show 1 · 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
avatar image Linkupdated · Jun 24, 2020 at 12:20 AM 0
Share

wow, thank you so much. A useful answer. :D

avatar image
1

Answer by elenzil · Sep 16, 2018 at 09:19 PM

the answer by @eses is definitely the one to use here. it's general-purpose and involves way less code to maintain. plus it will easily enable fancier gradients, like if you wanted to transition from green to white when health is above 95% or something.


however, for the Lerp()-curious, the reason your code is not behaving the way you want is that Lerp() needs a value between 0 and 1, but you're giving it values between 0 and 0.5 for the red -> yellow case, and 0.5 to 1.0 for the yellow -> green case.


for the red->yellow case, you should multiply by 2.0. for the yellow->green case, you should first subtract 0.5 and then multiply by 2.0.


a couple other unrelated notes:


1- i would suggest calculating a value like "normalizedHealth" in front of all your code - normalizedHealth = hpCurrent / hpMax. then use that for if if() statement and the math in the lerp()s. This small extra step will make your code a lot easier to debug / maintain.


2- assuming healthBar is a Unity.Engine.UI.Slider, there's no need to cast Value or MaxValue to floats. they're already floats. (but again, i recommend not referencing those values, in favor of a single 'normalizedHealth' value)


3- even if you did need to cast value and maxValue to floats, there would be no need to include that additional cast on (float)value/(float)maxValue. division of a float by a float will result in a float.

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

Answer by colourcrisis · Sep 16, 2018 at 10:06 PM

It's a bit messy and weird. But I used this, and it seems to work!

         int N = 4;
         float M = 1.5f;
         float healthCalc = (float)(((float)HitPoints / (float)TotalHitPoints)) * 100f;
         float N_root = (float)Mathf.Pow((healthCalc / 100f), (1f / M));
         float N_power = (float)Mathf.Pow((healthCalc / 100f), N);
 
 
         if (healthCalc < 50)
         {
             healthBar.BarColor = Color.Lerp(Color.red, Color.yellow, (float)N_root);
         }
         else if (healthCalc >= 50)
         {
             healthBar.BarColor = Color.Lerp(Color.yellow, Color.green, (float)N_power);
         }
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
avatar image
0

Answer by AndBje · Mar 28 at 04:01 PM

Love the Gradient since it is flexible, but if you want to do a simple C# line you can go from green-yellow-red like this (s is a float value between 0 and 1):

   new Color(2.0f*s, 2.0f*(1-s), 0);
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

159 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 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 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 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 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

How to animate fog color over time for day night cycle 1 Answer

Why Color.Lerp or Color32.Lerp doesnt work properly? 1 Answer

Time.deltaTime getting faster each time 2 Answers

How do I convert speed and heading to a vector? 3 Answers

Get color between 2 colors 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