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?
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);
 }
 
              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!
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
 
             Follow this Question
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