Fading in and out

Hey guys! Extreme beginner to Unity here. I am having trouble finding the right command for fading in and out.
Basically what I want is for the screen to fade to black, the player to move to a different location, then fade back in again. But I also want to keep the fading in and out as a separate function so as I can call on it to activate anytime I wish, without moving the players location.

I am trying to simulate the feeling of sleepiness. The players eyes will droop occasionally and they must explore the environment around them for clues as to whether they are now in a dream, or still in reality. Not every droop will make them fall asleep. So here is what I have so far:

var wakeUpCount : int = 0;
var dreamState01 : boolean;
var realityCountup : float = 0;

function Update () {
		
//Turns on Dreamstate and freezes the realityContup value when it reaches over 0.999
	if(realityCountup >= 0.999)
		dreamState01 = true;
	
	if(dreamState01 == true)
		realityCountup = 0.999;
	
//Moves the player to the first dream state when dreamState01 is true
	if(dreamState01 == true)
		transform.position = Vector3(20,0,0);
		
	//Keeps the player in reality when dream state is not on
	if(dreamState01 == false)
		transform.position = Vector3(0,0,0);

//Makes the realityCountup a random number when dreamstate is not on
	if(dreamState01 == false)
		realityCountup = Random.value;
		
//Decreases the wakeUpCount by 1 each frame 	
	if(wakeUpCount >= 1)
		wakeUpCount --;
	
//Disables the wake up count from going any higher than 0 when the player is in reality	
	if(dreamState01 == false)
		wakeUpCount = 0;
		
//Rapidly press "q" to switch from dream to reality
	if(Input.anyKeyDown)
		wakeUpCount += 9;
		
//Transitions the player from dream to reality when a number is reached		
	if(wakeUpCount >= 80)
		dreamState01 = false;
		
	if(wakeUpCount >= 80)
		realityCountup = 0;

}

EDIT: I am using this script for the actual fading

I have now created a fade function inside the script:

function droop () {
Camera.main.SendMessage("fadeOut");
yield WaitForSeconds (5);
Camera.main.SendMessage("fadeIn");

And that is giving me what I want so far. I now have to create something that will either kep the player in reality, or put them in a dream when the screen fades back

1 Answer

1

I ended up figuring it out myself:

var wakeUpCount : int = 0;
var dreamState01 : boolean;
var realityCountup : float = 0;
var droopCountup : float = 0;


function Start () {

realityCount ();

}

function realityCount () {

yield WaitForSeconds (10*Random.value);

realityCountup = Random.value;

if (realityCountup >= 0.5)
	droop ();
	
else
	Start ();

}

function droop () {

Camera.main.SendMessage("fadeOut");

yield WaitForSeconds (3);

droopCountup = Random.value;

if(droopCountup >= 0.5)
	transform.position = Vector3(20,0,0);
	
if(droopCountup >= 0.5)
	dreamState01 = true;

Camera.main.SendMessage("fadeIn");

Start ();

}

function Update () {

//Decreases the wakeUpCount by 1 each frame 	
if(wakeUpCount >= 1)
		wakeUpCount --;
	
//Disables the wake up count from going any higher than 0 when the player is in reality	
if(dreamState01 == false)
		wakeUpCount = 0;
		
//Rapidly press any keys to switch from dream to reality
if(Input.anyKeyDown)
		wakeUpCount += 9;
		
//Transitions the player from dream to reality when a number is reached		
if(wakeUpCount >= 80)
		dreamState01 = false;
	
if(dreamState01 == false)
		transform.position = Vector3(0,0,0);
		
}