- Home /
Guitext fade in/out timing with trigger?
Hi, I'm a real newbie with unity and JS in general so I apologise if this question has already been asked in a more professional form.
I'm creating a interactive narrative for a uni project with unity, and one of the elements of the experience is that when the player walks into a certain zone, narrative elements would fade in.
I am currently achieving this through triggers and fade in/out script on guitext.
However, I'm trying to get somewhat of a dialogue effect happening so guiText.text changes after a few seconds.
I'm running into a serious issue where if the player walks in and out repeatly of a trigger, the fade in/out effects start stacking up (as I'm using yield WaitForSeconds) and I get a really annoying flashing effect on the guitext.
I'm sure theres a much better way of doing this but I've been trying to get around it through if statements and what not but all I seem to do is make it worse...
Here's a example
var sayWhat = 1;
function OnTriggerEnter (collision : Collider) { if(collision.gameObject.tag == "Player"){ if(sayWhat == 1){ staticText2.TEXT = "Hmmm...?"; staticText2.FadeIN = true; yield WaitForSeconds(2); staticText2.FadeOUT = true; sayWhat = 2; }if(sayWhat == 2){ staticText2.TEXT = "I hear something..."; staticText2.FadeIN = true; yield WaitForSeconds(2); staticText2.FadeOUT = true; } } }
function OnTriggerExit (collision : Collider) {
if(collision.gameObject.tag == "Player"){
staticText2.FadeOUT = true;
}
}
and on the guitext's I have:
static var TEXT : String; static var FadeIN = false; static var FadeOUT = false;
function Update(){ guiText.text = TEXT; if (FadeIN == true) { FadeIn(); FadeIN = false; }else if (FadeOUT == true){ FadeOut(); FadeOUT = false; } }
function FadeIn(){
Fade.use.Alpha(guiText.material, 0.0, 1.0, 1.0, EaseType.In);
}
function FadeOut(){ Fade.use.Alpha(guiText.material, 1.0, 0.0, 1.0, EaseType.Out); }
It's okay when I have a few sentences but once I have a few it starts being really annoying...I have a feeling I'm TOTALLY going about this wrong way...so it would be great if someone could point me towards the right direction....Thanks in advance!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613081822im_/https://answers.unity.com/themes/thub/images/avi.jpg)