Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ZWLSOFTWARE · Dec 29, 2019 at 07:22 PM · coloralphaspriterenderermathf.lerp

how do i Mathf.lerp the SpriteRenderer.alpha?

         var colorAlpha = new Color32(255, 255, 255, 255);

         colorAlpha.a = (byte)Mathf.Lerp(255, 0, 1.5f * Time.deltaTime);
         ChestRender.color  = colorAlpha;
 

I'm trying to fade a image when it's clicked... Is there a way to mathf.lerp the spriterenderer alpha?

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

Answer by xibanya · Dec 30, 2019 at 02:07 AM

this'll do the trick.

 public IEnumerator SpriteFade(
 SpriteRenderer sr, 
 float endValue, 
 float duration)
 {
     float elapsedTime = 0;
     float startValue = sr.color.a;
     while (elapsedTime < duration)
     {
         elapsedTime += Time.deltaTime;
         float newAlpha = Mathf.Lerp(startValue, endValue, elapsedTime / duration);
         sr.color = new Color(sr.color.r, sr.color.g, sr.color.b, newAlpha);
         yield return null;
     }
 }

Remember to think of Lerp as having the first argument at 0 and the second argument at 100%. The third argument is where between 0 and 100% the value should fall. (You can also put an argument that's higher than 100% proportionally, but that's not going to be needed here.) elapsedTime / duration will get you the percentage of time completion of the blend. Since elapsedTime won't be higher than duration (since the while loop will stop if that happens) we can be confident that it will be between 0 and 1, making it ideal for blending between the two values.

Note that Unity's native Color struct uses color values between 0 and 1 rather than 0 and 255. I recommend going with Unity's native Color struct unless there's something specific you're trying to achieve with Color32 instead.

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 ZWLSOFTWARE · Jan 02, 2020 at 06:45 PM 0
Share

I was going for the color32 because it gives a value of 255. not 0 to 1 but this works great in my scene. Thanks!

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

120 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

Related Questions

Finding all sprite renderers in the scene and tell them to change color 1 Answer

How to change the alpha element of particles/additive color? 1 Answer

Using Color's alpha channel with value greater than one provides odd result 1 Answer

How to get Sprite color palette 1 Answer

Shader that does per Vert alpha with co 0 Answers


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