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 ?

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.