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 /
  • Help Room /
avatar image
0
Question by Sfbart96 · Nov 07, 2020 at 06:38 PM · unity 2dcoroutinesinvoke

How to Fade a Sprite after time has passed since it stops moving

Hello, I am trying to fade a sprite after some time has passed since the object stops moving. I tried finding it in other questions but I didn't find it.

Currently I have the following:

public class MoneyStates : MonoBehaviour { MoneyMovement moneyMovement; SpriteRenderer moneySprite; float alpha; bool isMoving;

 // Start is called before the first frame update
 void Start()
 {
     moneyMovement = GetComponent<MoneyMovement>();
     moneySprite = GetComponent<SpriteRenderer>();
     isMoving = false;

 }

 // Update is called once per frame
 void Update()
 {
     isMoving = moneyMovement.isMoving;
     if (isMoving)
     {
         alpha = 1f;
         moneySprite.color = new Color(moneySprite.color.r, moneySprite.color.g, moneySprite.color.b, alpha);

     } else
     {
         Invoke("Fade", 3);
     }
    
 }

 void Fade()
 {
     StartCoroutine(Devalue());
 }

   

 IEnumerator Devalue()
 {
     for (float f = 1f; f >= 0; f -= 0.01f)
     {
         alpha = f;
         moneySprite.color = new Color(moneySprite.color.r, moneySprite.color.g, moneySprite.color.b, alpha);
         yield return new WaitForSeconds(0.01f);

     }
 }

}

It is not really working. Do you guys have any suggestions?

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

Answer by Hellium · Nov 07, 2020 at 11:16 PM

 [Min(0)] public float DelayBeforeFade = 3;
 [Min(0.01f)] public float FadeDuration = 1;
 private MoneyMovement moneyMovement;
 private SpriteRenderer moneySprite;
 private float alpha;
 private float delay;

 // Start is called before the first frame update
 void Start()
 {
     moneyMovement = GetComponent<MoneyMovement>();
     moneySprite = GetComponent<SpriteRenderer>();
     alpha = moneySprite.color.a;
     delay = DelayBeforeFade;
 }

 // Update is called once per frame
 void Update()
 {
     if (moneyMovement.isMoving)
     {
         alpha = 1f;
         delay = DelayBeforeFade;
     }
     else if(delay <= 0)
     {
         alpha = Mathf.Max(alpha - Time.deltaTime / FadeDuration, 0);
     }
     else
     {
         delay -= Time.deltaTime;
     }

     moneySprite.color = new Color(moneySprite.color.r, moneySprite.color.g, moneySprite.color.b, alpha);
 }
Comment
Add comment · Show 2 · 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 Sfbart96 · Nov 08, 2020 at 12:04 AM 0
Share

Hi Hellium, thank you for your answer it works! I would like to understand what is the [$$anonymous$$in(0)] before the variable, it is the first time I see it. I would really appreciate it if you could explain it. Thank you!

avatar image Hellium Sfbart96 · Nov 08, 2020 at 12:16 AM 0
Share

Glad it worked.


The $$anonymous$$in attribute prevents the value from going below the specified value in the inspector. In this case, it's used to ensure the fade duration is greater than 0 (otherwise you would get a DivideByZero exception) and to ensure the delay is greater or equal to 0 (because negative duration does not make sense)


https://docs.unity3d.com/ScriptReference/$$anonymous$$inAttribute.html


There is also a $$anonymous$$ax attribute and a Range one (= value between $$anonymous$$ and max)

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

218 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Trying to Invoke method: Character_ControllerXD.SuperSpeedJump couldn't be called. 0 Answers

(SOLVED) Destroy(gameObject) doesn't destroy the gameObject, only disables behaviours. 0 Answers

The text in my game doesn't allign correctly depending on how big the screen is 0 Answers

can't add script behaviour TMP_SelectionCaret . The script need to derive from MonoBehaviour 4 Answers

How can i recreate transform.positionTo method? 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