- Home /
Airlock Door Code Problems [Solved]
var anim : String;
var AnimationSource = gameObject;
function OnTriggerStay()
{
if(Input.GetKeyDown(KeyCode.E))
animation.Play("AirlockOpen", PlayMode.StopAll);
}
Ok so this is my code. It works so far. Now i want 2 things.
1.) When my player enters the trigger zone i want to display a message (or even better a image) somewhere in the bottom center of the screen. Something like "Open/Close [E]". A picture would be better because i can stylize it more than a font.
2.) Somehow i have to check if the door is open. Maybe by setting a var when i play the animation to open and by setting it again when i play the animation to close. So my script know when to close by pressing "E" and when to open. Right now i have only one animation that opens the door. Do i need one to close it aswell or can i just play the "Open-animation" backwards?
I know i already had a topic for that code but for a different problem so i wasnt sure if i should post a new Q or use the old one.
Anyway, help is much appreciated, thanks.
PS: I'm from Austria so my English isn't the best.
Ok i solved it. This is the working code for anyone who needs it. I hope i explained everything in the code. Its JavaScript btw. Thanks to everyone who helped me.
var AnimationSource = gameObject;//Source of the animations used in the Script
var inside : boolean = false;//var that stores inside or outside of trigger // Used for Open/Close Hint
var image : Texture2D;//Texture of Open/Close Hint
var open : boolean = false;//var that stores Open/Close state of door
//----------------------------------------------------------------------
function OnTriggerEnter (col : Collider) //Displaying the Open/Close Hint
{
if(col.gameObject.name == "First Person Controller")//PlayerName
{inside = true;}
}
//---------------------------------------------------------------------
function OnTriggerStay (col : Collider)
{
if(col.gameObject.name == "First Person Controller")//PlayerName
{
if (Input.GetKeyDown(KeyCode.E)&& open == false && !animation.IsPlaying("AirlockClose"))//Closing the Airlock //Making sure that no other animation is playing
{
open = true;
animation.Play("AirlockOpen", PlayMode.StopAll);
}
else if (Input.GetKeyDown(KeyCode.E)&& open == true && !animation.IsPlaying("AirlockOpen"))//Closing the Airlock //Making sure that no other animation is playing
{
open = false;
animation.Play("AirlockClose", PlayMode.StopAll);
}
}
}
//-------------------------------------------------------------------------
function OnTriggerExit (col : Collider) //removing Open/Close Hint
{
if(col.gameObject.name == "First Person Controller")//PlayerName
{inside = false;}
}
//-------------------------------------------------------------------------
function OnGUI()//hint for opening/closing
{
if (inside == true)
{
GUI.Label(Rect(Screen.width/2-128,Screen.height-Screen.height/3,256,64), image);
}
}
Answer by slayer29179 · Dec 15, 2013 at 02:32 PM
Hello my friend! 1: You could use a Boolean variable when you stay inside the collider and turn it on and off. When its on you will use a GUI Box to show your image. For example:
var inside : boolean = false;
var image : Texture2D;
function OnTriggerStay (col : Collider) //Using col and collider you can make it work ONLY for the door and not for any other triggers. Col will store the object hit.
{
if(col.gameObject.name == "NAME_OF_COLLIDER")
{
inside = true;
if (Input.GetKeyDown("e"))
{
animation.Play("AirlockOpen", PlayMode.StopAll);
}
}
}
function OnGUI()
{
if (inside == true)
{//Rect = posx,posy,width,height
GUI.Box(Rect(0,0,64,64), image);
}
}
2: I don't think there is a way to reverse animations in Unity. But if you find something I would love to know! You can either: a) Use an external modelling / animating product to reverse the animation b) Code the door rotating c) Make another animation
I hope this helps! P.s. sorry the script is not formatted properly I can't make it work haha
Ok thanks i got that to work. But i still have 2 problems. 1.) The image doesn't disappear after i left the trigger.
Solved can i display the image without the Grey box? Solved
I used GUI.Lable ins$$anonymous$$d of Gui.Box
This is the code. I tried resetting the inside var if i am not inside the trigger but it doesn't work. I guess this function is only called when i am inside. Any idea how to make the image disappear when i left the trigger?
var anim : String;
var AnimationSource = gameObject;
var inside : boolean = false;
var image : Texture2D;
function OnTriggerStay (col : Collider) //Using col and collider you can make it work ONLY for the door and not for any other triggers. Col will store the object hit.
{
if(col.gameObject.name == "First Person Controller")
{
inside = true;
if (Input.Get$$anonymous$$eyDown("e"))
{
animation.Play("AirlockOpen", Play$$anonymous$$ode.StopAll);
}
}
else { inside = false; }
}
function OnGUI()
{
if (inside == true)
{//Rect = posx,posy,width,height
GUI.Box(Rect(Screen.width/2-128,Screen.height-Screen.height/3,256,64), image);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How To Make GUI Buttons Load/Quit 1 Answer
animated sword + script 1 Answer
Two Unity GUI questions 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers