Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Fade in/out a sprite on BoxCollider2D when Enter/Exit

Discussion in '2D' started by RuneShiStorm, Nov 21, 2022.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi all, I've made this script where I managed to make a Sprite Fade in/out when exit and enter a BoxCollider2D.
    However, If I don't wait until the IEnumerator is done, it will start glitching a flashing when exiting.
    In other words, if I walk in and out from the BoxCollider2D, the Sprite will start flashing. So I have to wait inside the BoxCollider2D until its done, then I can exit and it will fade back nicely.

    Any idea if there is another way to make this function or fix this one?

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. public class ExitEnterSprite : MonoBehaviour
    7. {
    8.     public SpriteRenderer sprite;
    9.  
    10.     private void OnTriggerEnter2D(Collider2D collision)
    11.     {
    12.         if (collision.GetComponent<Character>() != null)
    13.         {
    14.             StartCoroutine(FadeIn());
    15.         }
    16.     }
    17.     private void OnTriggerExit2D(Collider2D collision)
    18.     {
    19.         if (collision.GetComponent<Character>() != null)
    20.         {
    21.             StartCoroutine(FadeOut());
    22.         }
    23.     }
    24.     private IEnumerator FadeIn()
    25.     {
    26.         float alphaVal = sprite.color.a;
    27.         Color tmp = sprite.color;
    28.         while (sprite.color.a > 0)
    29.         {
    30.             alphaVal -= 0.01f;
    31.             tmp.a = alphaVal;
    32.             sprite.color = tmp;
    33.             yield return new WaitForSeconds(0.05f); // update interval
    34.         }
    35.     }
    36.     private IEnumerator FadeOut()
    37.     {
    38.         float alphaVal = sprite.color.a;
    39.         Color tmp = sprite.color;
    40.         while (sprite.color.a < 1)
    41.         {
    42.             alphaVal += 0.01f;
    43.             tmp.a = alphaVal;
    44.             sprite.color = tmp;
    45.             yield return new WaitForSeconds(0.05f); // update interval
    46.         }
    47.     }
    48. }
    49.  
    50.  
    51.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Cheers, I saw the link you provided but it doesn't make sense when only bits of the code is written. Where does the code go? And how is it connected to the Sprite? And how is it connected to the BoxCollider2D?
    CHeers though!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    I'm not sure how much simpler it can be. I put some extra code examples in the above post.

    The fading is just the guts of an otherwise-empty MonoBehaviour in charge of fading.

    EDIT: here's a direct example used to set volume of music. Make the effort to build a bridge in your mind that setting a volume level in music is 100% analogous to fading a color or image or anything else:

    (in this case MyMusic.volume is substituted for the currentVolume, saving one variable)

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // @kurtdekker - fade music up / down
    4. // ONLY manipulate DesiredVolume
    5.  
    6. public class FadeMusic : MonoBehaviour
    7. {
    8.     [Header( "Put the AudioSource in here")]
    9.     public AudioSource MyMusic;
    10.  
    11.     [Header( "Set this volume; ONLY manipulate this")]
    12.     [Range( 0.0f, 1.0f)]
    13.     public float DesiredVolume;
    14.  
    15.     [Header( "How fast to change?")]
    16.     public float RateOfChange = 0.5f;
    17.  
    18.     void Start()
    19.     {
    20.         MyMusic.volume = DesiredVolume;
    21.     }
    22.  
    23.     void Update ()
    24.     {
    25.         // broken into three lines for clarity:
    26.  
    27.         float vol = MyMusic.volume;
    28.  
    29.         vol = Mathf.MoveTowards( vol, DesiredVolume, Time.deltaTime * RateOfChange);
    30.  
    31.         MyMusic.volume = vol;
    32.     }
    33. }