• 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
2
Question by Dismortus · Feb 21, 2014 at 11:23 PM · c#color.lerp

Lerp between 3 colors based on energy amount.

Hi all, I have a simple gradient texture I'm using for an energy bar. I start with full energy, and green color for the bar. I can't quite figure out Color.Lerp. I want to fade from green, to yellow, to red as the energy runs down, and smoothly back to green as energy increases. I can get color to fade to another, but can't quite come up w/ a proper formula to address my needs...

Comment
Add comment · Show 1
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 Mikozouzou · Aug 24, 2015 at 11:47 AM 0
Share

Thanks to both of you, the addition of your both answer work really well !

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiloblargh · Feb 22, 2014 at 12:13 AM

You just need to split it up into 2 different Lerps:

 if (health < .5) { 
     blendColor = Color.Lerp (Color.red, Color.yellow, health*2);
     } else {
     blendColor = Color.Lerp (Color.yellow, Color.green, (health-.5)*2);
     }
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 Scribe · Feb 21, 2014 at 11:58 PM

The basic pseudo code would be:

 var maxEnergy : flaot;
 var energy : float;
 var greenColor : Color;
 var redColor : Color;
 
 Color.Lerp(redColor, greenColor, energy /maxEnergy);

Though you will need to put the last line in a method, presumably the Update function.

Scribe

EDIT:

just reread your question and completely missed the 3rd color part, it was 1 in the morning :S

@Kiloblargh's answer is perfect (you could replace health with the energy/maxEnergy)

Sorry about that!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

Spawned Object Gradient Mesh Colors? 2 Answers

How can I loop through all colours in code? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges