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 The_sito · Nov 22, 2013 at 05:00 AM · colorlerprendereralphaadditive

renderer.material.color not being changed?

Hey guys, I got a pretty simple problem to solve, but I'm not sure what I'm doing wrong.

I am trying to make an object "fade out" based on distance. These objects are being rendered on their own camera, and will be clipped out at a certain distance, however its pretty jarring without any fading. My plan is to Lerp their color from gray to black (they are set to Additive, so black = transparent) just before they are clipped. This way, they will appear to "fade out."

However, my code doesn't produce any results.

Here is my code:

 {
     public Transform camR;
 
     // Update is called once per frame
     void Update () 
     {
         float dist = Vector3.Distance(camR.position, transform.position);
         print ("Distance to CamR:" + dist);
 
         if(dist >= 20.0f)
         {
             renderer.material.color = Color.Lerp(Color.grey, Color.black, Time.deltaTime);
 
         }
         else if (dist <20.0f)
         {
             renderer.material.color = Color.Lerp(Color.black, Color.grey, Time.deltaTime);
         }
     }
 }

Any help is greatly appreciated :)

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

Answer by yogee · Nov 23, 2013 at 04:29 AM

try this

 var duration = 5.0;
  void Update () 
     {
     var t : float = Mathf.InverseLerp (0, duration, Time.time);
         float dist = Vector3.Distance(camR.position, transform.position);
  
        if(dist >= 20.0f)
        {
          renderer.material.color = Color.Lerp(Color.grey, Color.black, t);     
        }
        else if (dist <20.0f)
        {
          renderer.material.color = Color.Lerp(Color.black, Color.grey,t);
        }
     }
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 Eric5h5 · Nov 22, 2013 at 05:05 AM

The third parameter in Lerp is a float between 0 and 1. It's like a percentage. Time.deltaTime will normally be a very small number, so your code will pretty much always return a color near Color.grey (in the first Lerp) or Color.black (in the second Lerp). I would suggest using a percentage based on distance rather than Time.deltaTime.

Comment
Add comment · Show 4 · 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 The_sito · Nov 23, 2013 at 02:36 AM 0
Share

Hey Eric5h5,

Thanks for the reply. I replaced time.deltatime with 0.7f to see if there was a change. Unfortunately, nothing changed.

Is there a better way to get an additive material to change color over time?

avatar image Eric5h5 · Nov 23, 2013 at 02:42 AM 0
Share

I suggested using a percentage based on distance; 0.7 is just a constant and naturally wouldn't do anything except return a value 70% of the way between grey and black.

avatar image The_sito · Nov 23, 2013 at 03:11 AM 0
Share

What's a way to express that? dist / X ?

Sorry about my lack of knowledge here. I'm primarily a game artist and trying to figure a lot of this out as I go :p I appreciate the help.

avatar image Bunny83 · Nov 23, 2013 at 04:32 AM 0
Share

One way is to use $$anonymous$$athf.InverseLerp to get a 0-1 value from the distance. Something like that:

 float t = $$anonymous$$athf.InverseLerp(15, 20, dist);
 renderer.material.color = Color.Lerp(Color.grey, Color.black, t);

this would set the color to grey if dist is between 0 and 15, it would lerp from grey to black between 15 and 20 and stay black if the distance is greater than 20.

btw, you don't need the if statements in this case. This fully covers it ;)

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

19 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

Related Questions

Changing two different objects renderer colour 1 Answer

Smooth changing transparency of particle/additive shader 2 Answers

Renderer.material not cloning? 3 Answers

Change alpha value to transparent when colliding 1 Answer

changes to colour alpha affect RGB values, but not in the same way 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