• 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
Question by ASZYK · May 08, 2014 at 02:14 AM · shadercolorlerpboolcolor.lerp

Lerping 2 values on a shader on button press.

Hello there, I'm having a little trouble with Lerping 2 values on a shader.

The effect I'd like to happen is that when a button is pressed, the object illuminates instantly and then fades down to dark

I've set up 2 public variables which act as the 2 colours I want to Lerp between (bright and notBright) and added them to a variable that is active when "T" is pressed. The variable they are added to then fills in the value for the colour of the shader "_Emission". The Lerp time is set with objBrightSpeed. I've added a boolean to check whether it happens and return a true/false so it only happens once.

The script is throwing up no errors but the object colour starts off as intended and then illuminates on press but fails to lerp to the darker colour, it just stays lit constantly from then on.

Here's what I've scripted for it:

   public float objBrightSpeed = 0.2f; // This determines the speed at which it de-illuminates
 
     public bool block01lBrightLerp = false;
     public Color block01lBrightness; // This is the Color variable that is the object colour.
     public Color block01lBright = new Color (1.0f, 1.0f, 1.0f, 1.0f); // This adds illumination to an object
     public Color block01lNotBright = new Color (0.0f, 0.0f, 0.0f, 0.0f); // This removes illumination from an object
         
         
         
 void Update ()     
     { 
 
         
 
             if (block01lBrightLerp == false) // Makes sure that this function only calls once 
             {    
                 block01l.renderer.material.SetColor("_Emission", block01lBrightness);
                 block01lBrightness = Color.Lerp(block01lBright, block01lNotBright, Time.deltaTime * objBrightSpeed);
                 block01lBrightLerp = true;
             }
 
         }
         if (Input.GetKeyUp(KeyCode.T))
         {
             
             block01lBrightLerp = false;
 
         }
 }

  • Edited as one of the variables didn't seem to show in the post

if anyone could give me a pointer with this it'd be much appreciated!

Comment

People who like this

0 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 LunaArgenteus · May 08, 2014 at 04:32 AM 0
Share

I'm not sure what the rest of your update function looks like, but there are a couple things I'd like to point out.

First, Lerp takes a value between 0 and 1 as it's third parameter, returning some value between the first and second parameter depending on what that is. It looks like you're passing a value that doesn't ever increase to Lerp - right now you'd always get whatever Time.deltaTime objBrightSpeed happened to be, where instead you probably meant to store a progress variable that increments from 0 to 1 BY Time.deltaTime objBrightSpeed and use that as your third parameter, ensuring a smooth transition from 0 to 1 (and thus from block01lBright to block01lNotBright).

Secondly, from what I can see right now, it looks like you have the brightness set to change only when block01lBrightLerp is false, but are changing it to be true immediately upon detecting that it is false. I can't see the rest of your update function, but this probably means you're only changing the value of block01lBrightness once per release of the "T" key (where you set the lerp boolean to true).

Both of these would prevent you from achieving the smooth transition you're looking for.

avatar image ASZYK · May 08, 2014 at 06:08 AM 0
Share

Hi LunaArgenteus, thanks for responding.

I'm still very much a newcomer to C# so forgive me if I anything I write seems obvious/stupid.

In the first part you mention about a stored progress variable - is this the part that now reads 'public float objBrightSpeed = 0.2f;' I've edited the post to show that as for some reason it had split my code in 2 and chopped that line off.

In response to the second part, this is all of the update function related to this. My aim was that on the press of T it instantly changes the colour to bright and then lerps back to notbright slowly, regardless of whether the button is held or not.

In regards to my thinking - it was that having it start at false means it would allow that lerp to happen, setting it to true once it's happened would mean that it wouldn't keep updating. Having it set to false on key Up would then allow the update to be called again if T is pressed again.

Regarding smooth transition - I intended it to be instant on the press and then smooth on the way out. It's meant to be able to simulate the hitting of a piano key if that helps to put it into a bit of context.

0 Replies

· Add your reply
  • Sort: 

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

Color.lerp not working properly 2 Answers

lerp transparency back and forth, while cycling through rainbow? 0 Answers

Color.Lerp doesn't work 1 Answer

How do you Lerp light.color into multiple colors? 2 Answers

Color.Lerp everytime Onclick 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