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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Fade out problem.

Discussion in 'Scripting' started by Henris088, Jun 23, 2015.

  1. Henris088

    Henris088

    Joined:
    Jun 4, 2015
    Posts:
    6
    Hello I want unity to fade out from red to clear if hurt == true, I use this code:

    Code (csharp):
    1.  
    2. function OnGUI()
    3. {
    4. if(hurt)
    5.     {
    6.     GUI.color = Color.red;  
    7.     GUI.color.a = Mathf.Lerp(1, 0, 4*Time.deltaTime);    
    8.     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), faderTexture);
    9.     }
    10. }
    11.  
    but the screen becomes red and no fading happens (Actually I increased Lerp timing to 4, and now I can see screen blinking, changing opacity up and down) , I`m pretty sure that happens because I dont turn "hurt" variable to false. BUUUUUUT
    I have tried typing hurt = false; in the end of the code aswell as WaitForSeconds(4) then hurt = false, but then nothing appears on the screen at all.
    Please help. :)
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    image28, sluice and Korno like this.
  3. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    This seriously needs to be a sticky. Just this. With the title "Lerp Problems".
     
    image28 likes this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    because the code tag sticky works :p
     
    image28, GroZZleR and Korno like this.
  5. Henris088

    Henris088

    Joined:
    Jun 4, 2015
    Posts:
    6
    Ok ok. Maybe my lerp is all wrong. I have also tried this code from the web:
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]#pragma strict
    4. [*]// FadeInOut
    5. [*]var fadeTexture :Texture2D;
    6. [*]var fadeSpeed =0.2;
    7. [*]var drawDepth =-1000;
    8. [*]privatevar alpha =1.0;
    9. [*]privatevar fadeDir =-1;
    10. [*]functionOnGUI(){
    11. [*]alpha += fadeDir * fadeSpeed *Time.deltaTime;
    12. [*]alpha =Mathf.Clamp01(alpha);
    13. [*]GUI.color.a = alpha;
    14. [*]GUI.depth = drawDepth;
    15. [*]GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), fadeTexture);
    16. [*]}
    17. [/LIST]
    18.  
    19.  
    (sorry for poor pasting, cant be bothered to fix it..) anyways, same thing happens.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I can't really be bothered to make a useful contribution to this thread either.
     
  7. Henris088

    Henris088

    Joined:
    Jun 4, 2015
    Posts:
    6
    Why even reply then? :):):)
     
  8. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Welcome to Unity Forum and you successfully put yourself in the position of not being supported with this kind of behaviour... This forum is not about googling for a code, not understanding it but thinking, it could work and pasting it here so someone else figures out the code for you... That behaviour becomes kind of common in here, and sorry, but anyone able to help you won't do it with this attitude. Cheers
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I was trying to be clever and funny and teach you a valuable lesson about getting help on the forums. Since it didn't work I'll be more explicit. If you can't be bothered to make it easy for people to help you, why should any of the community volunteers take time out of their life to help you? There are plenty of other people willing to put in some effort.
     
  10. Henris088

    Henris088

    Joined:
    Jun 4, 2015
    Posts:
    6
    Dying of laughter, bro.

    [*] in front of each line makes it really hard to read I guess...

    Because some nice and clever people rather ''take time out of their life'' while helping someone than discussing some unnecessary *poop*. :) Whatever, have a good day.
     
  11. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Try changing:
    Code (csharp):
    1.  
    2. GUI.color.a = alpha;
    3.  
    to:
    Code (csharp):
    1.  
    2. GUI.color = Color(1.0, 1.0, 1.0, alpha);
    3.  
    And get rid of the GUI.depth = drawDepth line.
     
  12. Henris088

    Henris088

    Joined:
    Jun 4, 2015
    Posts:
    6
    @GroZZleR cheers man, I`ve just tried it - no luck.
    Looks like nothing changes at all. I have debug logged the "alpha" though and the values keeps changing from something like:
    0.99 - 0.98, goes up and down all the time.

    If you got any other ideas I would be pleased to hear em ^^ anyways ty.
     
  13. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Henris088 likes this.
  14. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
    Code (CSharp):
    1. {
    2.    StartCoroutine(Fading(0f, 1f, 5f));
    3. }
    4.  
    5. IEnumerator Fading(float p, float q, float duration) {
    6.     float time = 0f;
    7.     while (time < duration) {
    8.        time += Time.deltaTime;
    9.        ????.alpha = Lerp(p, q, time, duration);
    10.        yield return null;
    11.     }
    12.     ????.alpha = q;
    13. }
    14.  
    15. float Lerp(float p, float q, float time, float duration) {
    16.     if (duration == 0f)
    17.        return q;
    18.     return Mathf.Min(1f, Mathf.Lerp(p, q, time/duration));
    19. }
     
    Henris088 likes this.
  15. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This script 100% works for me:


    I moved a bit of it to Update instead of OnGUI so it's more consistent (OnGUI can be called multiple times per frame):
    Code (js):
    1.  
    2. #pragma strict
    3.  
    4. public var fadeTexture : Texture2D;
    5. public var fadeSpeed : float = 0.2;
    6.  
    7. private var alpha : float = 1.0;
    8. private var fadeDir : float = -1;
    9.  
    10. function Update()
    11. {
    12.   alpha += fadeDir * fadeSpeed * Time.deltaTime;
    13.   alpha = Mathf.Clamp01(alpha);
    14. }
    15.  
    16. function OnGUI()
    17. {
    18.   GUI.color = Color(1.0, 1.0, 1.0, alpha);
    19.   GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
    20. }
    21.  
    If this doesn't work, there's something else wrong.
     
    Henris088 likes this.
  16. Henris088

    Henris088

    Joined:
    Jun 4, 2015
    Posts:
    6
    GREAT help guys, thank you all. ^^
    @GroZZleR this one works perfectly <3