I need help getting a camera to fade up from black after my player teleports to next room position.
Here's my script, I need the game to fade from clear to black when I press the E key. Then fade from black to clear once my player changes transform positions. Any ideas? thanks!
#pragma strict
var fPlayer : Transform;
var doorSound : AudioClip;
var nextRoom : Transform;
var doorSoundPlay : boolean = false;
function Start ()
{
}
function OnTriggerStay(Col : Collider)
{
if(Col.tag == "Player")
{
if(Input.GetKey(KeyCode.E))
{
fPlayer.transform.position = nextRoom.position;
GetComponent.<AudioSource>().PlayOneShot(doorSound);
doorSoundPlay = true;
}
}
}
function OnTriggerExit(Col : Collider)
{
if(Col.tag == "Player")
{
doorSoundPlay = false;
}
}
function Update ()
{
}
You could use a UI Image with no sprite, just black color, and when you hit E, start a coroutine that fades its alpha from 0 to 1.
Answer by toddisarockstar · Jan 14, 2016 at 09:12 PM
var col:Color;
col=Color(0,0,0,0);
var tx:Texture2D;
tx= new Texture2D(1,1);
var fadeblack:boolean;
function OnGUI () {
GUI.color=col;
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),tx);}
function Update(){
if(fadeblack) {if(col.a<1){col.a=col.a+Time.deltaTime;}}
if(!fadeblack){if(col.a>0){col.a=col.a-Time.deltaTime;}}
if(Input.GetKeyDown("e")){fadeblack=!fadeblack;}
}
put this script in your game somewhere and push the "e" key. Customize it as you need.
I don't this will work because is you look at my script I need the fade to black to happen after "fPlayer.transform.position = nextRoom.position;" and I can't call a guifunction inside "function OnTriggerStay" and I can't call it after trigger stay either. :(
this code is tested! and it works to fade in and out. it was an example how to fade. in this code the only thing you need to do is change my fadeblack variable
if you add all this to your script. simple make fadeblack=true to fade to black and make fadeblack=false to fade back in!!! change the one variable anywhere you would like in your code.
the the last line i wrote asking for "e" simply toggles fadeblack to true and false. for example purposes.
so just make my last line say fadeblack=true when button is pushed.
when your trigger happens just say fadeblack=false;
Answer by bathorsthroat · Jan 15, 2016 at 02:32 AM
BUMP
Ok I got it to fade but it will only fade the first time it is triggered, I need is to trigger the fade every time "fPlayer.transform.position = nextRoom.position" is called. Here's my script??
var fPlayer : Transform;
var doorSound : AudioClip;
var nextRoom : Transform;
var doorSoundPlay : boolean = false;
var fadeTime : float = 5;
var fadeTexture : Texture;
var fadeOut : boolean = false;
var tempTime : float;
var time : float = 0.0;
var fadeOutSound : AudioClip;
var camera :GameObject;
function Start ()
{
fadeOut = false;
GetComponent.<AudioSource>().clip = fadeOutSound;
GetComponent.<AudioSource>().Play();
}
function OnTriggerStay(Col : Collider)
{
if(Col.tag == "Player")
{
if(Input.GetKey(KeyCode.E))
{
fadeOut = true;
fPlayer.transform.position = nextRoom.position;
GetComponent.<AudioSource>().PlayOneShot(doorSound);
doorSoundPlay = true;
}
}
}
function OnTriggerExit(Col : Collider)
{
if(Col.tag == "Player")
{
doorSoundPlay = false;
}
}
function Update ()
{
if (fadeOut){
if(time < fadeTime) time += Time.deltaTime;
tempTime = Mathf.InverseLerp(0.0, fadeTime, time);
}
if(tempTime >= 1.0) enabled = false;
}
function OnGUI(){
if(fadeOut){
GUI.color.a = 1 - tempTime;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}
}
Your answer
Follow this Question
Related Questions
How to fade in and out two logos inside the same scene? [c#] 1 Answer
Animation Not Playing 0 Answers
How to fade in and out two panels in a scene? (c#) 1 Answer
How to fade in and out a image in a loop?,How do I fade in and out image on a loop? 0 Answers
Audio Fade In / Fade Out with Trigger 0 Answers