How to Build a Unity Door System Without Trigger Chaos

Door logic looks trivial until you add enemy AI, replay logic, and multiple colliders. Then one trigger event opens the wrong door at the wrong time. Here is a cleaner structure.

Step 1: Move door rules into a state machine

public enum DoorState { Closed, Opening, Open, Closing }

public class DoorController : MonoBehaviour {
    public DoorState State { get; private set; } = DoorState.Closed;
}

Step 2: Debounce trigger events by actor ID

private readonly HashSet<int> activeActors = new();

void OnTriggerEnter(Collider other) {
    if (!activeActors.Add(other.GetInstanceID())) return;
    RequestOpen();
}

Step 3: Separate animation from permission checks

bool CanOpen(Player p) => p.HasKeycard && !isLockedDown;

Pitfalls

  • Trigger code directly changing animation parameters everywhere.
  • No actor tracking, causing repeated open/close spam.
  • Combining auth checks with transition timing.

Validation

  • One actor entering once creates one open request.
  • AI and player interactions do not conflict.
  • Replay mode reproduces door sequence deterministically.

Get New Tutorials by Email

No spam. Just clear, practical breakdowns you can apply right away.

Enjoy this tutorial?

Get new practical tech tutorials in your inbox.