Trigger to open door with keypress

I started working with Unity3d and scripting last week. I am trying to make a door open when the player presses “Enter”. I have a rectangular trigger on each side of the door. I attached the door_open animation to the outside door trigger and door_close animation to the inside door trigger. The door is also tagged “door”. I’m guessing I’ll have to attach 2 javascripts to the player called “door_open” and “door_close”. Here is my “door_open” script thus far (attached and below). It has errors still. Any help would be greatly appreciated.

function Update ()
{
function OnTriggerEnter (collision : Collider)
{

if(collider.gameObject.tag==“door” Input.GetKeyDown(“Enter”));
{

hit.collider.gameObject.animation.Play(“door_open”);
}

}

}

function OnTriggerEnter (collision : Collider) 
{

if(collider.gameObject.tag=="door"  Input.GetKeyDown("Enter"))
{	

hit.collider.gameObject.animation.Play("door_open" );
}

}

The trigger code should be outside of the update() and you add ; at the if loop

you dont need 2 scripts to make it open and close just put 1 on the door :slight_smile:

function OnTriggerEnter (Player : Collider)
{
	if(Player.gameObject.tag == "Player"  Input.GetKeyDown("Enter"))
	{
		animation.CrossFade("Open");
	}
}

function OnTriggerExit (Player : Collider)
{
	if(Player.gameObject.tag == "Player")
	{
		animation.CrossFade("Close");
	}
}

And use something like this :slight_smile:

PS its not tested :slight_smile:

You can use CompareTag to compare it’s tag and it’s faster also.