Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

fading color with respect to health bar increase

Discussion in 'Scripting' started by sharpg, Mar 27, 2015.

  1. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    hello guys,
    i want to fade dust object with a Towel object.the scenario is as health bar increase the dust remove gradually.when health bar get full the dust fade completely.please guide me how can i do that?.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can lerp colours. Or you can overlay a transparent texture and fade that out.
     
  3. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    If you want to cross fade between two images, lerp is the way to go. You can try something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CrossFade : MonoBehaviour
    5. {
    6.     private SpriteRenderer s1;
    7.     private SpriteRenderer s2;
    8.  
    9.     public float fadeLevel = 0.5f;
    10.  
    11.     Color fadeIn;
    12.     Color fadeOut;
    13.  
    14.  
    15.     void Start () {
    16.         s1 = GameObject.Find("Mars").GetComponent<SpriteRenderer>();
    17.         s2 = GameObject.Find("Jupiter").GetComponent<SpriteRenderer>();
    18.  
    19.         fadeIn = fadeOut = Color.white;
    20.  
    21.         fadeOut.a = 0;
    22.     }
    23.    
    24.     void Update () {
    25.         s1.color = Color.Lerp(fadeIn, fadeOut, fadeLevel);
    26.         s2.color = Color.Lerp(fadeOut, fadeIn, fadeLevel);
    27.     }
    28. }
    29.  
     
  4. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thanks All :)
    I am using following code and its solve the problem.
    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4. usingUnityEngine.UI;
    5.  
    6. publicclassDustFade : MonoBehaviour {
    7.  
    8. publicImagehbFront;
    9.  
    10. voidStart()
    11. {
    12. hbFront.fillAmount=0;
    13. }
    14. voidOnTriggerEnter2D(Collider2Dother)
    15. {
    16.  
    17.  
    18. if(other.collider2D.tag=="Towel")
    19. {
    20. renderer.material.color =Color.Lerp (renderer.material.color, Color.clear,Time.deltaTime*10f);
    21. hbFront.fillAmount=hbFront.fillAmount + 0.10f;;
    22. }
    23. }
    24.  
    25.  
    26.  
    27. }
    28.