- Home /
GUI script problem with trigger enter.
Hello,
Im having problem with a script I'm using with GUI, Its with a door open script when the player enters the trigger a GUI will show up saying "Press 'E' to open the door" when the player leaves the trigger the GUI will go away but if you open the door and then leave the trigger the GUI will not go away and will say up till the player closes the door. Could anyone help please.
Below is the script I'm using.
var smooth = 2.0;
var DoorOpenAngle = 90.0;
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
function Update (){
if(open){
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}else{
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("e") && enter){
open = !open;
}
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'E' to open the door");
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
Thank you for reading my post and I hope you have a great day.
Answer by zharik86 · May 28, 2014 at 06:43 PM
As far as I understood, after door opening, the message that he closed a door shall be always visible to the player. Independent, whether to be the player in a zone of this door or not. If so, simply add in GUI:
function Start() {
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
//Init open variable
open = false; //door is not open - state close
}
function OnGUI() {
if(enter && !open) {
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'E' to open the door");
} else if (open) {
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Close door");
}
}
I hope it to you will help.
This scripted only replaces the problem with "Close door" on the screen at all times when the door is open.
I won't it so if the player opens the door then walks away the "Press 'E' to open the door" is not on the screen I don't won't any GUI on the screen when the door is open and the payer is off the trigger. But thank you anyway for trying to help.
Your answer
Follow this Question
Related Questions
4.6 Trigger.js Wrong command is running. Help? 0 Answers
Loading a scene in game with a GUI 1 Answer
Trigger GUI working wrong 1 Answer
activate gui on trigger enter 1 Answer
OnTriggerEnter problem 3 Answers