vrijdag 1 april 2016

Juicing

This one will be rather short. But in a project I recently made we wanted a fade to white effect at every beginning and end of a level. I made a rather hacky way achieve this.
For this effect you need a plane that is in front of the camera. This is your fade 'layer'. Than for the script portion you need a public boolean called fadeOut and a public Color called color.

In the Start function you set the fadeOut boolean to false. Also set the color to the material color that the renderer picks up.

 void Start () {
  fadeOut = false;
  color = GetComponent ().material.color;
  level = Application.loadedLevel;
 }


Then in the update you check if fadeOut is true. If that's true you increase the alpha value of the color, making it visible. Then when the alpha value is 1 or more, you can run some logic like load the next level in my case.

Else (if fadeOut is false) you decrease the alpha value. When the alpha value is 0 you set it to 0, so it doesn't go way past 0.
 
void Update () 
 {
  if (fadeOut == true) 
  {
   color.a += 0.05f;
   GetComponent ().material.color = color;

   if (color.a >= 1) {
    level += 1;
    Application.LoadLevel (level);
   }
  } 
  else 
  {
   color.a -= 0.05f;
   GetComponent().material.color = color;
  }
  if (color.a <= 0)
  {
   color.a = 0;
  }
 }

To make this work you only need to set the fadeOut bool to true when you want the screen to fade to white. If the plane is already visible it will fade from white, because fadeout will be false.



Geen opmerkingen:

Een reactie posten