- Home /
Door - Press button to open
Hi everyone. I'm trying to make a game but im stuck in the door opening. I want to like hit the button e and the door open then if i want hit e again and close it. Can someone tell me all the steps to do it ?
Answer by Kiloblargh · May 23, 2013 at 12:51 AM
You should have a game controller script, and a script that goes on the player, and a script that goes on the door.
In the game controller script, make a reference to the door script to be assigned later:
var thisDoor : DoorScript;
then a script on your player object that has a reference to the game controller script:
var gameController : ControlScript;
The player object needs a collider attached, and the door needs to be tagged "Door" and have a trigger collider in front of it.
The player object's script should have an OnTriggerEnter which tells the game controller what door you are standing in front of.
function OnTriggerEnter (other : collider)
{
if (other.gameObject.tag == "Door")
{
gameController.thisDoor = other.gameObject.GetComponent (DoorScript);
}
}
DoorScript needs a variable to tell the door's current state:
var isOpen : System.Boolean = false;
Then back in your control script, you need this in Update:
function Update()
{
if (Input.GetKeyDown("e"))
{
if (thisDoor.isOpen)
{
thisDoor.Close();
}
else
{
thisDoor.Open();
}
}
}
What the DoorScript functions "Open()" and "Close()" will do is left to you. For starters, try just setting the door object inactive or active and once that works, then get it to animate.
If none of the above made sense to you, read the manual some more.
Sorry for the delay. I can't make the door move normaly all it does is rotates on the spot it's on. I saw a tutorial on youtube for how to make an openable door and he created a gameobject and placed in where te door rotates. I did the same but nothing happens. Any help on that ?
You have to make sure to update the script references to the hinge object's transform ins$$anonymous$$d of the original door object's transform, which should be a child of the hinge object.
I added a box collider on the door and moved it a bit left where the door leans, The movement is preety good but when i animate it, it goes crazy.
Your answer

Follow this Question
Related Questions
Simple Script - and no result 2 Answers
Change button icon for each without multiple GUISkins 1 Answer
help with door script! 3 Answers
GUI Button not working...but the the script is correct... 2 Answers
Making multitouch button 0 Answers