- Home /
GUI.Box that will appear for 3sec
I'm trying to make a GUI.Box appear for 3 seconds, when entering a Collider (For like Zones and such). But i seem to not be able to understand the timer. Here's what i've got so far:
var guiOn : boolean;
var guiSkin : GUISkin;
var zoneName : String;
var size : Rect = Rect(0,0,100,100);
var textTimer : float = Time.deltaTime * 2;
static var deltaTime : float;
function Start () {
}
function Update () {
if(textTimer==2){
textTimer +1;
}
}
function OnTriggerEnter(theCollider : Collider) {
guiOn=true;
}
function OnGUI () {
GUI.skin = guiSkin;
if(guiOn) {
GUI.Box(size,zoneName);
}
}
I'm still trying to make it work myself, but it would be AWESOME with some help :)
Answer by ArkaneX · Oct 28, 2013 at 04:14 PM
Basically you need to reset your timer at trigger enter, increase it inside Update, and display text if it is less or equal than 3 seconds. So something like this:
function Update() {
if(textTimer <= 3) {
textTimer += Time.deltaTime;
}
}
function OnTriggerEnter(theCollider : Collider) {
textTimer = 0;
}
function OnGUI() {
if(textTimer <= 3) {
GUI.Box(size,zoneName);
}
}
I dont think it'll work anyway.. i just tested it.. and it seems like once i face away from the object with the collider.. then the timer resets (as if i didnt enter it)
If the timer resets, then it looks like the collision was triggered again (unless you set timer to 0 anywhere else in your code).
Additionally, code from my answer is just a simple example, and you should probably modify it to suit your needs. For example, I don't know how exactly you're using your script - if you dynamically add it to a game object, and timer is initialized to 0, then your GUI will be visible. To prevent this, you have to add some additional conditions.
The change you added works fine, and just the way i wanted it to work. But once i face away from the object with the script, then it resets...
So it looks like your player object exited collision area for a moment and then entered it again. Is your player collider rectangular, with length greater than width? In this case when you collided, rotating can cause OnTriggerExit and then OnTriggerEnter again.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
GetComponentInChildren(Renderer).active wont work? 2 Answers
Faux Gravity Prolem? #2 2 Answers
Help| Convert a javascript to C# 1 Answer