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 /
  • Help Room /
avatar image
0
Question by Nsingh13 · Aug 31, 2015 at 01:49 AM · c#2d-platformercolorlerpspriterenderer

Color.lerp for Spriterender is not smooth

The script below works, but it does not fade, it just jumps straight to the color.

 using UnityEngine;
 using System.Collections;
 
 
 public class Ladder : MonoBehaviour {
 
     public float smooth = 50;
     public Color transparent = new Color(1f,1f,1f,0f);
     public Color visible = new Color(1f,1f,1f,1f);
     public SpriteRenderer fadeimage;
     public GameObject boy;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
     
 
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         StartCoroutine ("fade");
         boy.transform.position = new Vector2 (8f, 11f);
     }
 
     public IEnumerator fade(){
         fadeimage.color = Color.Lerp (transparent, visible,Time.deltaTime * smooth);
         yield return new WaitForSeconds (1);
         fadeimage.color = Color.Lerp (visible, transparent,Time.deltaTime * smooth);
     }
 
 }

How can I make it fade? Thanks in advance! :)

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 darthtelle · Aug 31, 2015 at 01:15 PM

I'm not sure if you fully understand how lerping and coroutines work. A lerp is something which can happen over a period of time between the values (percentages) of 0 and 1. Unity tutorial link. Your coroutine doesn't update anything for an entire second. Coroutines can BASICALLY behave like a second Update() function or a secondary thread. Unity tutorial link.

 private IEnumerator Update_Fade()
 {
 float timer = 0.0f;
 float time = 1.0f;
 
 while(timer <= time)
 {
 timer += Time.deltaTime;
 float lerp_Percentage = timer / time;
 
 fadeimage.color = Color.Lerp(transparent, visible, lerp_Percentage);
 
 yield return null;
 }
 }

I wasn't actually going to write you a solution, because I believe you need to understand first how both methods work, but I hope you won't just copy and paste the answer but actually take the time to understand what's happening. Because this type of function is something I personally use EVERYWHERE. But to do so, you need to understand! :) Hope it helps!

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 Nsingh13 · Aug 31, 2015 at 11:51 PM 0
Share

Thank you for your detailed response. I will work on it soon and get back to you :-)

avatar image Nsingh13 · Sep 01, 2015 at 06:16 AM 0
Share

Thanks! it works! But don't worry, I didn't just copy and paste. in fact I learned a lot from your info. Accepted!

avatar image darthtelle · Sep 01, 2015 at 07:00 AM 0
Share

@Nsingh13 Brilliant! That's what I like to hear!! Glad I could help. :)

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

28 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

Related Questions

Color.Lerp over HP 1 Answer

Changing image color from other object's script (glitch)? 0 Answers

How to animate fog color over time for day night cycle 1 Answer

Animator controller probably messed up, need advice 0 Answers

2D-Platformer wrong jumps 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