The, as yet unnamed game I am currently building in Unity3D has need of doors. So I wrote a script for doing just that. I'm putting the door script out here for the benefit of any poor sap who may come across it. I hope it allows you to make your game faster than I did.

The following is the main code. Below it I'll explain a few bits.

1:  using UnityEngine;  
2:  using System.Collections;  
3:  public class Door : MonoBehaviour {  
4:       GameObject door1;  
5:       GameObject door2;  
6:       GameObject LockedCollider;  
7:       GameObject LockedDoors;  
8:       GameObject Unlocked;  
9:       bool doorClosed = true;  
10:       bool doorClosing = false;  
11:       bool doorLocked = false;  
12:       bool closeAndLock = false;  
13:       void Start () {  
14:            door1 = transform.Find("Unlocked/Door1").gameObject;  
15:            door2 = transform.Find("Unlocked/Door2").gameObject;  
16:            Unlocked = transform.Find("Unlocked").gameObject;  
17:            LockedCollider = transform.Find("LockedCollider").gameObject;  
18:            LockedDoors = transform.Find("LockedDoors").gameObject;  
19:            AstarPath.active.UpdateGraphs (transform.Find("Bulkhead1").collider2D.bounds);  
20:            AstarPath.active.UpdateGraphs (transform.Find("Bulkhead2").collider2D.bounds);  
21:       }  
22:       void Update () {  
23:       }  
24:       public void updateAStar() {  
25:            AstarPath.active.UpdateGraphs (LockedCollider.collider2D.bounds);  
26:       }  
27:       public void OpenDoor(){  
28:            if(doorLocked)  
29:                 return;  
30:            doorClosed = false;  
31:            iTween.MoveTo(door1,iTween.Hash("position", new Vector3(-0.35f,0,0.1f),"islocal", true,"time", 1));  
32:            iTween.MoveTo(door2,iTween.Hash("position", new Vector3(0.35f,0,0.1f),"islocal", true,"time", 1));  
33:       }  
34:       public void CloseDoor(){  
35:            doorClosing = true;  
36:            iTween.MoveTo(door1,iTween.Hash("position", new Vector3(-0.11f,0,0.1f),"islocal", true,"time", 0.5f));  
37:            iTween.MoveTo(door2,iTween.Hash("position", new Vector3(0.11f,0,0.1f),"islocal", true,"time", 0.5f, "onComplete", "CloseFinished", "onCompleteTarget", this.gameObject));  
38:       }  
39:       private void CloseFinished(){  
40:            doorClosed = true;  
41:            doorClosing = false;  
42:            if(closeAndLock) {  
43:                 closeAndLock = false;  
44:                 LockDoor ();  
45:            }  
46:       }  
47:       public void LockDoor() {  
48:            if(doorLocked)  
49:                 return;  
50:            if(!doorClosed) {  
51:                 closeAndLock = true;  
52:                 CloseDoor();  
53:                 return;  
54:            }  
55:            Unlocked.SetActive(false);  
56:            LockedDoors.SetActive(true);  
57:            LockedCollider.SetActive(true);  
58:            doorLocked = true;  
59:            updateAStar();  
60:       }  
61:       public void UnlockDoor() {  
62:            if(!doorLocked)  
63:                 return;  
64:            Unlocked.SetActive(true);  
65:            LockedDoors.SetActive(false);  
66:            LockedCollider.SetActive(false);  
67:            doorLocked = false;  
68:            updateAStar();  
69:       }  
70:       public void SensorTripped(){  
71:            if(doorClosed)  
72:                 OpenDoor();  
73:            if(doorClosing) {  
74:                 iTween.Stop(this.gameObject,true);  
75:                 //WaitForSeconds(0);  
76:                 OpenDoor();  
77:            }  
78:       }  
79:       /*void OnGUI() { // Used for Debug  
80:            if (GUI.Button(new Rect(10, 70, 50, 30), "Open"))  
81:                 OpenDoor();  
82:            if (GUI.Button(new Rect(10, 100, 50, 30), "Close"))  
83:                 CloseDoor();  
84:            if (GUI.Button(new Rect(10, 130, 50, 30), "Lock"))  
85:                 LockDoor();  
86:            if (GUI.Button(new Rect(10, 160, 50, 30), "Unlock"))  
87:                 UnlockDoor();  
88:       }*/  
89:  }  

The following Image shows the structure of the prefab that this script takes advantage of:

  • "Door" is just an empty at 0,0,0 with the above script attached.
  • The "Bulkhead" objects are the walls on ether side of the door. They each have their own 2D Box Collider for use by the pathfinding system.
  • "LockedCollider" is an empty object with just a Box Collider 2D. This gets enabled and disabled to help update the Actor pathing system.
  • "Unlocked" is just an empty used to enable and disable both door objects easier. 
  • The "Door" are just Sprites. no colliders.These are the objects that are moved using iTween to give the illusion of doors that open and close.
  • "Locked Doors" is just an empty used to enable and disable both "DoorPanel" objects easier
  • the "DoorPanel" objects are just sprites with a single position. they are ether enabled or disabled to show the locked state of the door.
  • "DoorSensor" is an empty with a collider covering the area where an actor will trigger the door to automatically open. It also has a very simple script on it called DoorSensor.cs the code can be found below.



1:  using UnityEngine;  
2:  using System.Collections;  
3:  public class DoorSensor : MonoBehaviour {  
4:       void OnTriggerEnter2D() {  
5:            transform.parent.GetComponent<Door>().SensorTripped();  
6:       }  
7:       void OnTriggerStay2D() {  
8:            transform.parent.GetComponent<Door>().SensorTripped();  
9:       }  
10:       void OnTriggerExit2D() {  
11:            transform.parent.GetComponent<Door>().CloseDoor();  
12:       }  
13:  }  

This script accesses the Door object and calls the above functions to make the door open and close when an Actor trips the door sensor.

My door looks like this in action:


If you have questions or find bugs in the code, let me know and I'll be glad to help.