Door Open/Close Script + Animation
We needed doors that the player can open so that certain areas could be blocked at the start so I made a rectangle in maya and animated it opening.
Then I imported the door into unity and set up two animation takes so that I had two different states for the door being closed and opening. Essentially one was a 1-frame animation and the other was a 28-frame animation.

I did this so I had control over the two states and I could reference them when it came to coding.
After setting all this up the next step was to get the door to open with coding:
//put this script on both door and door trigger
var DoorGameObject : Transform; // This is a reference to the door GameObject that has your animation
private var HasTriggerBeenUsed : boolean = false; // This is to make sure that the button is not repeatedly pressed.
private var doorOpen : boolean = false;
public var myFirstPersonPickUp : FirstPersonPickUp;
public var DoorTrigger : GameObject;
public var CanOpen : boolean = false;
function OnTriggerEnter(otherObj: Collider)
{
{
Debug.Log("Can Open With Key + E");
CanOpen = !CanOpen;
}
}
function OnTriggerExit (otherObj: Collider)
{
{
Debug.Log("Cannot Open");
CanOpen = false;
}
}
function Start ()
function Update () {
if (Input.GetKeyDown("e") && HasTriggerBeenUsed == false && CanOpen == true) {
DoorGameObject.animation.Play("Doormove");
HasTriggerBeenUsed = !HasTriggerBeenUsed;
Debug.Log("Door open");
}
else if (Input.GetKeyDown("e") && HasTriggerBeenUsed == true && CanOpen == true) {
Debug.Log("Can't open door.");
}
}
This code basically says, if the E button is pressed within the trigger area and it hasn’t been opened before, it will open and play the door movement animation.








