• 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 pdunton · Feb 01, 2014 at 06:08 AM · colorlerp

Color.Lerp is not working

I have a cube that I want to shift slowly from white to red, then from red to white. Right now, it just flickers very fast between the two without and sign of a Lerp. I'll post the code. !whiteToRed means that it is going from red to white. Also, there are no Errors in the Console. Thanks!

 #pragma strict
 var red : Color = new Color(1, 0, 0);
 var white : Color = new Color(1, 1, 1);
 public var speed : float = 100;
 var whiteToRed : boolean = true;
 
 function Start () {
 
 }
 
 function Update () {
     if(whiteToRed){
         renderer.material.color = Color.Lerp(white, red, Time.deltaTime * speed);
     }
     if(!whiteToRed){
         renderer.material.color = Color.Lerp(red, white, Time.deltaTime * speed);
     }
     whiteToRed = !whiteToRed;
 }
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

2 Replies

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

Answer by robertbu · Feb 01, 2014 at 06:26 AM

To start with let me give you an alternate bit of code that will cycle between white and red. It uses Mathf.PingPong() to cycle the colors:

 var red : Color = new Color(1, 0, 0);
 var white : Color = new Color(1, 1, 1);
 public var speed : float = 0.5;
  
 function Update () {
        renderer.material.color = Color.Lerp(white, red, Mathf.PingPong(Time.time * speed, 1.0));
 }

As for your code, there are a couple of different ways to use Lerp. When the end points are fixed as you have here, you need to vary the last parameter between 0.0 and 1.0...that is you would need to create some sort of timer. There a use of Lerp that produces an eased movement towards the goal. Your speed would have to be much lower, and you would use it like:

 renderer.material.color = Color.Lerp(renderer.material.color, red, Time.deltaTime * speed);

Also your code flipping 'whiteToRed' every frame. You would only flip it when the color is at or near the destination color.

Comment
Add comment · Show 3 · 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 pdunton · Feb 01, 2014 at 10:18 PM 0
Share

I decided to use your code, ins$$anonymous$$d of $$anonymous$$e. It works a lot better and uses less memory. Thanks!

avatar image pdunton · Feb 01, 2014 at 10:25 PM 0
Share

Is there a way to make it stay white for a little longer?

avatar image robertbu · Feb 01, 2014 at 11:36 PM 0
Share

With this method ($$anonymous$$athf.PingPong()) the time it is red and the time it is white will be the same. You can adjust 'speed' to slow things down. To make different times for each color, you will need approach the problem differently...more like your original code.

avatar image
1

Answer by Cassos · May 02, 2020 at 11:56 PM

Already solved but I found the problem if anybody cares:

Color.Lerp works like this:

 Color.Lerp(current, target, delta * speed)

But you didnt assign the current as the color of the renderer but just "red". That causes the color to always be the same.

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

21 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

Related Questions

single color lerp 1 Answer

Fading an object with Color.Lerp 1 Answer

Color lerp once? 2 Answers

Unity2D - How do you Color.lerp a SpriteRenderer sprite to white? 1 Answer

Change color between 2 values mid game. 1 Answer


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