Fade to black

I would like the screen to fade to black when the player dies. I already have a script which fades the screen IN from black, but I admit I did not write this script so i’m not sure what I need to change to reverse its effect, and also its written in javascript which i’m even more unfamiliar with.

#pragma strict
     
// FadeInOut
     
var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;
     
private var alpha = 1.0;
private var fadeDir = -1;
     
function OnGUI(){
         
    alpha += fadeDir * fadeSpeed * Time.deltaTime;  
    alpha = Mathf.Clamp01(alpha);   
         
    GUI.color.a = alpha;
         
    GUI.depth = drawDepth;
         
    GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}

First, I’ll say this is OnGui. If you don’t use OnGui for your other stuff, I’d suggest you look into something different.

I would use a fullscreen image object and a tweening engine like LeanTween or Dotween to fade the image in or out. It would give you the same effect and be easier to control in my opinion.

Now, what this bit of code you are displaying is doing is fading a Texture from alpha 0 to alpha 1. Chances are the texture is black, so all it’s doing is making itself visible.

The reverse is to have a texture that is already visible and do alpha -= so it reduces alpha from 1 to 0.

But again, use the other method I mentioned and I think it will be easier to work with.

Got this from wiki( I guess…) the author uses guiText to show fade in and out. Replace GuiText with your gameobject. Hope this helps. ^~^
Also your script above is Javascript, however the code says it is C# this below is in C#

///////////////////////////////////////////// ///Zapp's Uber fading timer script/////////// ///////////////////////////////////////////// ///v3.141//////////////////////////////////// ///////////////////////////////////////////// ///Brought to you by the letter Z..////////// ///////////////////////////////////////////// ///And a grant from the MyWallet Foundation// /////////////////////////////////////////////
using UnityEngine; using System.Collections;

public class Timer : MonoBehaviour {

public int minutes; public int seconds; public int fraction;

public float startTime; float FadeStartTime; float FadeTime;

public float playTime; public float FadeSpeed;

public Color TimerColor = new Color(1,1,1,0);

bool go = false; public bool fadeIn = false; public bool fadeOut = false;
void Start ()
{
if( !guiText )
{
Debug.Log("This timer requires a GUIText component");
enabled = false;
return;
}
if(FadeSpeed == 0)
{
FadeSpeed = 1;
}
guiText.material.color = TimerColor;
}
void Update ()
{
//This is for testing, make sure to remove it after your done.
if(Input.GetMouseButtonDown(0))
{
if(!go)
{
StartTimer();
}
Fade();
}
if(go)
{
playTime = Time.time - startTime;
minutes = (int)(playTime/60f );
seconds = (int)(playTime % 60f);
fraction = (int)((playTime *10) %10);
guiText.text = string.Format("{0}\'{1}\"{2}", minutes, seconds, fraction);
}
if(fadeIn)
{
FadeIn();
}
if(fadeOut)
{
FadeOut();
}
}
public void StartTimer()
{
startTime = Time.time;
go = true;
FadeStartTime = Time.time;
fadeIn = true;
}
public void StopTimer()
{
go = false;
}
public void Fade()
{
if( !fadeIn && !fadeOut )
{
FadeStartTime = Time.time;
if(TimerColor.a == 1)
{
Debug.Log(TimerColor.a);
fadeOut = true;
}
else
{
guiText.enabled = true;
fadeIn = true;
}
}
else
{
FadeStartTime = Time.time - ((1 - FadeTime) / FadeSpeed);
fadeIn = !fadeIn;
fadeOut = !fadeOut;
}
}
void FadeIn()
{
FadeTime = (Time.time - FadeStartTime) * FadeSpeed;
TimerColor.a = Mathf.SmoothStep(0, 1, FadeTime );
guiText.material.color = TimerColor;
if(TimerColor.a == 1)
{
fadeIn = false;
}
}
void FadeOut()
{
FadeTime = (Time.time - FadeStartTime) * FadeSpeed;
TimerColor.a = Mathf.SmoothStep(1, 0, FadeTime );
guiText.material.color = TimerColor;
if(TimerColor.a == 0)
{
fadeOut = false;
}
}
}

Wow. All sorts of crazy flashback in this thread. Don’t use OnGUI. Don’t use a GuiTexture. Both are old and essentially deprecated for in game use.

Instead use a canvas with an image, and lerp the Alpha on the image.

Like so:

3 Likes

Yep, that’s how we did it for Chesster, and it works a treat.

With CrossFadeAlpha, this is pretty much a one-liner. (Haven’t watched the above video, so I don’t know if that’s how it does it or not.)

Yeah, we use LeanTween for our work projects to handle fades, as we can also add an oncomplete that will switch to the new scene when the fade is done or to do additional stuff when the scene is done “fading in”.