How to make a door Open the Correct way

using UnityEngine;
public class PushDoor :
MonoBehaviour{[Header(“Door Settings”)]
public float openAngle = 90f;
public float openSpeed = 180f;
private bool opening = false;
private float targetAngle = 0f;
private float currentAngle = 0f;
private Quaternion startRotation;

void Start()
{
    startRotation = transform.localRotation;
}

void Update()
{
    if (!opening) return;

    currentAngle = Mathf.MoveTowards(currentAngle, targetAngle, openSpeed * Time.deltaTime);

    transform.localRotation = startRotation * Quaternion.Euler(0, currentAngle, 0);
}

private void OnTriggerEnter(Collider other)
{
    if (opening) return;
    GetComponent<Collider>().isTrigger = false;
    if (!other.CompareTag("Player")) return;

    Vector3 playerDir = other.transform.position - transform.position;

    float side = Vector3.Dot(playerDir, transform.forward);

    // open away from player
    targetAngle = side > 0 ? -openAngle : openAngle;

    opening = true;
}

}‘’’
the door seems to just randomly decide which way to open ive mucked around with it for a while any ideas how to fix this?

The code looks okay. Perhaps you’ve not got the door orientated correctly and its transform.forward direction isn’t pointing along the correct axis. The forward/blue axis should be pointing towards or away from the player when they touch the door.

Or your player moves very quickly and is sometimes passing through to the other side of the door as its triggered, resulting in the door thinking the player came in the opposite direction.

In 2026, I would not bother making a procedural door opener.

Use animations, one of them for each side of the door and one for closed, tween between them.

Here’s my reference one-sided door solution. You can use it as reference, or else hack it updirectly to support both side openings.

DoorOpenClose.unitypackage (7.3 KB)