- Home /
How to fade in onGUI on trigger enter/exit? Gui shows on trigger, but no fade.
Have this script from a YT tutorial by SpeedTutor:
#pragma strict
private var guiShow : boolean = false;
var fadeColor : Texture;
function OnTriggerStay (Col : Collider)
{
if(Col.tag == "Player")
{
guiShow = true;
}
}
function OnTriggerExit (Col : Collider)
{
if(Col.tag == "Player")
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.DrawTexture(Rect(Screen.width / 1000.0, Screen.height / 1000.0, 2048, 1024), fadeColor);
}
}
Pops a gui texture from enter until exit. Would like to fade texture "in" on enter, then "out" on exit. For instance, during a respawn or as a "flash" effect. I'm a scrub at code and have only recently started playing around with scripting, my knowledge of syntax, order and structure is very poor. If someone could help me out as to which function to put where in order to acheive the desired effect, that would be splendid.
TIA
Answer by TonyLi · Oct 21, 2013 at 01:37 PM
GUI.color sets the global tinting for the GUI. It's an (r,g,b,a) value, where a is the alpha (transparency). If you set it to something less than one, the GUI will be semi-transparent.
To fade in or out, you need to gradually change the alpha value. You can use coroutines to do this, as in the code below. The FadeIn() and FadeOut() coroutines fade over 1 second. I made them short to keep the example clear. In practice, you'll probably want to modify the coroutines to update the alpha value every frame and use a variable to specify the fade duration. The Coroutines manual page has an example that does this.
private var guiShow : boolean = false;
var fadeColor : Texture;
var currentGUIColor : Color;
function OnTriggerStay (Col : Collider)
{
if ((Col.tag == "Player") && !guiShow)
{
FadeIn();
}
}
function OnTriggerExit (Col : Collider)
{
if ((Col.tag == "Player") && guiShow)
{
FadeOut();
}
}
function OnGUI()
{
if (guiShow)
{
var originalColor = GUI.color;
GUI.color = currentGUIColor;
GUI.DrawTexture(Rect(Screen.width / 1000.0, Screen.height / 1000.0, 2048, 1024), fadeColor);
GUI.color = originalColor;
}
}
function FadeIn()
{
guiShow = true;
for (var i = 0; i < 10; i++)
{
currentGUIColor = new Color(1,1,1, i/10);
yield WaitForSeconds(0.1f);
}
currentGUIColor = new Color(1,1,1,1);
}
function FadeOut()
{
for (var i = 10; i > 10; i--)
{
currentGUIColor = new Color(1,1,1, i/10);
yield WaitForSeconds(0.1f);
}
currentGUIColor = new Color(1,1,1,0);
guiShow = false;
}
[1]: http://docs.unity3d.com/Documentation/Manual/Coroutines.html
Thanks for the input. It's still not fading as I'd like, but this gives me something to play with and look into. Again, thank you and if you have any more advice/help please feel free.
Your answer
Follow this Question
Related Questions
Application LoadLevel on Input.KeydownEnter TextField 0 Answers
fade in out multi images then go to next scene 1 Answer
Why can't void be used in this context? 2 Answers
Transitions When Pausing? 1 Answer
Fade In GUI on proximity 1 Answer