Hello Unity forums, I have an issue that is holding me back from progressing with my work and would greatly appreciate any help.
Right now, I am working on a message system in my game that would display pointers/tips/quests etc to the player via GUI boxes when the player is within a trigger zone. I have a huge collider over the area which is set to “On Trigger” to draw out the area whereby the message should be displayed, and have a script applied to this collider which should turn on when the player is within the area. However, the trigger codes aren’t working as I think they should.
Here is my code.
function OnTriggerEnter(other: Collider){
if(other.tag == "Player"){
displayOn = true;
}
}
function OnTriggerExit(other: Collider){
if(other.tag == "Player"){
displayOn = false;
}
}
// GUI Function
function OnGUI(){
if(displayOn){
GUI.Box(Rect(Screen.width/2 - offsetX, Screen.height/2 - offsetY,boxX,boxY), message);
}
}
While I have no issues turning the display on, I have issues with turning the display off with this approach. It’s like the boolean is stuck and does not turn to false on trigger exit. When I had a variable which adds 1 when i enter, and had a variable which minuses 1 when I exit, the results were never 0 and it would seem that I am having multiple entry triggers happening. I have searched vigorously and I came across threads that said that trigger exit has had bug issues and might this be an issue?
After reading another article, I have changed my code to use OnTriggerStay and time instead to display my message.
function OnTriggerStay(other: Collider){
if(other.tag == "Player"){
timerStart = true;
enterTime = Time.time;
}
}
if(Time.time - enterTime > displayTime){
displayOn = false;
timerStart = false;
}
else{
displayOn = true;
}
While this works to a certain extent, I am also getting weird results with this. Even if the player is standing in the trigger zone, it seems the variable enterTime is only updated when the player is moving while within the trigger zone, and not while the player is just sitting in the area. As far as I know, enterTime should be continuously updated while he is within the area, right?
I have spent quite an amount of time on this and have no idea how to solve this and would greatly appreciate any help. Thanks in advance.
PS: I’m using Unity 3.5.6f and my trigger zone has a rigidbody applied to it already.