- Home /
Fading out GUITexture - I need help with timer function that will control the speed of fading out
Hi Everyone,
I am writing a script to fadeout a GUITexture (and a few other things) after an input from the keyboard has been received. I am having trouble with decreasing the alpha value for the GUITexture slowly, at the moment the GUITexture fades out almost instantly. I have seen similiar questions on this topic and the solutions seems to be with using co-routines. I do not understand how to write coroutines in JS.
My fadeout function is similiar to Zerofractals fade function. I have also looked at Mathf.Lerp and mathf.SmoothDamp, however I could not achieve what I wanted. Perhaps I am using the functions incorrectly or not as intended. Any help on this would be much appreciated.My code is as follows
var scrollTexture:GUITexture;
var messageToGamePlayer: GUIText;
var scrollText: GUIText;
var fadeDuration:float = 0.5;
var alpha:int;
private var timeLeft:float=0.5;
function Start () {
scrollTexture.enabled=false;
messageToGamePlayer.enabled=false;
scrollText.enabled=false;
}
function OnTriggerEnter(){
this.renderer.enabled=false;
scrollTexture.enabled=true;
messageToGamePlayer.enabled=true;
scrollText.enabled=true;
}
function Update (){
if (Input.GetButton ("Jump")){
fadeOut();
// disableGUITexture();
// Destroy(gameObject);
}
}
function fadeOut(){
if(scrollTexture.color.a > 0){
timeLeft = timeLeft - Time.deltaTime;
alpha = (timeLeft/fadeDuration);
scrollTexture.color.a=alpha/2;
}
}
function disableGUITexture(){
scrollTexture.enabled=false;
messageToGamePlayer.enabled=false;
scrollText.enabled=false;
}
Thanks in Advance.
Answer by youngapprentice · Jan 12, 2013 at 08:04 PM
A Coroutine is a function that shares control with the rest of the script, and releases control on the 'yield' statement.
To make a coroutine, you just make a new function and make sure it has a yield (otherwise it wouldn't need to be a coroutine) then us the code 'StartCoroutine( MyFunction( params ));'
A Lerp script I use is this:
private var lerp = 0.0;
private var track = 0.0;
private var value : float;
var time : float;
function lerpPos(){
if(track < time){
track += Time.deltaTime;
lerp = track/time;
value = Mathf.Lerp(start, end, lerp);
}
}
And obviously your value would be the alpha. Another possibility for you is iTween.FadeTo()