Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jackthomasennis · Sep 21, 2021 at 01:39 PM · beginnerlerpspriterenderertransitions

How Do I Smoothly Change The Color Of The SpriteRenderer Over Time?

Hi friends, thanks for taking the time to look at my question.

I have a "tile" that when clicked fluctuates between an "on" state and "off" state. As a visual cue, I would like the tiles to have an "on color" and an "off color".

My understanding is that I would need to use Color.Lerp - lerping between the sprite's current color and the desired color.

I've done so in my code here,

public class testclick : MonoBehaviour { bool isOn = true; public Color on_color; public Color off_color; private SpriteRenderer sr; private float timeLeft = 0f;

  void Awake()
  {
      sr = gameObject.GetComponent<SpriteRenderer>();
  }
 
  void Update()
  {
      Debug.Log("Updating...");
      if (timeLeft > 0)
      {
          Debug.Log($"IsOn: {isOn}");
          if (isOn)
          {
              Color c = Color.Lerp(on_color, sr.color, timeLeft);
              sr.color = new Color(c.r, c.g, c.b, 3);
              Debug.Log($"{c.r}, {c.g}, {c.b}");
          }
          else
          {
              Color c = Color.Lerp(off_color, sr.color, timeLeft);
              sr.color = new Color(c.r, c.g, c.b, 3);
              Debug.Log($"{c.r}, {c.g}, {c.b}");
          }
          timeLeft -= Time.deltaTime;
      }
  }
 
  void OnMouseDown()
  {
      timeLeft = 0.2f;
      isOn = !isOn;
  }

}

but what results is NOT a smooth transition. What do I need to do to make this more fluid, and less choppy?

Thanks again!

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
1
Best Answer

Answer by Hellium · Sep 21, 2021 at 01:56 PM

The 4th argument of the Color.Lerp function is meant to be a float between 0 and 1.

If this argument is 0, the function returns the 1st color

If this argument is 1, the function returns the 2nd olor

If this argument is a value between 0 and 1, the function returns a color computed as a blend between the 1st and 2nd color.


In your case, it seems that timeLeft can will be equal to 0.2 at most.


So, either ensure this variable can reach 1 and just change the "speed" the value change:

   void Update()
   {
       Debug.Log("Updating...");
       if (timeLeft > 0)
       {
           Debug.Log($"IsOn: {isOn}");
           if (isOn)
           {
               Color c = Color.Lerp(on_color, sr.color, timeLeft);
               sr.color = new Color(c.r, c.g, c.b, 3);
               Debug.Log($"{c.r}, {c.g}, {c.b}");
           }
           else
           {
               Color c = Color.Lerp(off_color, sr.color, timeLeft);
               sr.color = new Color(c.r, c.g, c.b, 3);
               Debug.Log($"{c.r}, {c.g}, {c.b}");
           }
           timeLeft -= Time.deltaTime * 5; // To keep a lerp time of 0.2 seconds
       }
   }
  
   void OnMouseDown()
   {
       timeLeft = 1f;
       isOn = !isOn;
   }


OR, get rid of Color.Lerp and timeLeft and use Vector4.MoveTowards instead, because you can convert a Color from and to Vector4

   void Update()
   {
       Debug.Log($"IsOn: {isOn}");
       if (isOn)
       {
           Color c = Vector4.MoveTowards(on_color, sr.color, Time.deltaTime * 5);
           sr.color = new Color(c.r, c.g, c.b, 3);
           Debug.Log($"{c.r}, {c.g}, {c.b}");
       }
       else
       {
           Color c = Vector4.MoveTowards(off_color, sr.color, Time.deltaTime * 5);
           sr.color = new Color(c.r, c.g, c.b, 3);
           Debug.Log($"{c.r}, {c.g}, {c.b}");
       }
   }
  
   void OnMouseDown()
   {
       isOn = !isOn;
   }
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 jackthomasennis · Sep 21, 2021 at 11:59 PM 0
Share

You're the best!

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

142 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

Related Questions

How to Lerp between two materials 1 Answer

I'm used to Java/Processing, some help with new() please 2 Answers

Why won't my blocks fall? 1 Answer

How to use Lerp? 3 Answers

Smooth Transform with Lerp? 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