Automatic Door

I wanna make an automatic door that opens outwards from the player. So for now I’ve been using the 2 script below.

Script 1:

public class newdooropen : MonoBehaviour
{
    Animator anim;
    bool playerstillhere = false;
    void Start() {
       anim= GetComponent<Animator>();

    }
    IEnumerator MyFunction()
    {
      
        
        yield return new WaitForSeconds(10f);
        playerstillhere = false;
        anim.SetBool("PlayerIsThere", playerstillhere);

        // enter your code here
    }
    void Update()
    {
        

    }
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            bool doorOpen = true;

          playerstillhere = true;

            anim.SetBool("PlayerIsThere", playerstillhere);
            anim.SetBool("DoorStatus", doorOpen);
        }
    }
    void OnTriggerExit(Collider another)
    {
        if (another.tag == "Player")
        {



            StartCoroutine(MyFunction());

        }

    }

}

Script 2:

public class newdooropen2 : MonoBehaviour
{
    Animator anim;
    bool playerstillhere = false;
    void Start()
    {
        anim = GetComponent<Animator>();

    }
    IEnumerator MyFunction()
    {


        yield return new WaitForSeconds(10f);
        playerstillhere = false;
        anim.SetBool("PlayerIsThere", playerstillhere);

        // enter your code here
    }
    void Update()
    {


    }
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            bool doorOpen = false;

            playerstillhere = true;

            anim.SetBool("PlayerIsThere", playerstillhere);
            anim.SetBool("DoorStatus", doorOpen);
        }
    }
    void OnTriggerExit(Collider another)
    {
        if (another.tag == "Player")
        {



            StartCoroutine(MyFunction());

        }

    }

}

The problem I am having is that. I have either one game object with a script above and the other script in the game object as a sub. So the collider box does not move as a unit for one of them. Not sure if I explained that clearly enough.

But overall. I just need help / advice on making a automatic door that opens outwards from the player.
For Example, Player A (Left side) goes near the door[Door opens 90 degree to the right] vice versa if player A is on the right side[Door opens to the left 90 degrees].

You do not need two scripts. One script will suffice if you do it something like this

using UnityEngine;
using System.Collections;

public enum DoorState  {Opened, Closed};
public enum DoorAnimationState  {None, Opening, Closing};
public class AutomaticDoorOpen : MonoBehaviour
{
    public bool reverse = false;
    
    Animator anim;
    bool playerstillhere = false;
    
    DoorState doorState = DoorState.Closed;
    DoorAnimationState doorAnimationState = DoorAnimationState.None;
    
    bool animating = false;

    void Start()
    {
        anim = transform.parent.gameObject.GetComponent<Animator>();
        doorState = DoorState.Closed;
        doorAnimationState = DoorAnimationState.None;
    }

    IEnumerator MyFunction()
    {
        yield return new WaitForSeconds(10f);
        playerstillhere = false;
        anim.SetBool("PlayerIsThere", playerstillhere);
        // enter your code here
    }

    void Update()
    {
        if ((anim.GetCurrentAnimatorStateInfo(0).IsName("Layer_Door.CloseDoor")) || (anim.GetCurrentAnimatorStateInfo(0).IsName("Layer_Door.OpenDoor")))
        {
            animating = true;
        }
        else
        {
            animating = false;
            doorAnimationState = DoorAnimationState.None;
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if ((other.tag == "Player") && (doorState == DoorState.Closed) && (doorAnimationState == DoorAnimationState.None))
        {
            doorAnimationState = DoorAnimationState.Opening;
            doorState = DoorState.Opened;
            animating = true;
            if (reverse)
            {
                anim.SetTrigger("Open Door Reverse");
            }
            else
            {
                anim.SetTrigger("Open Door");
            }
        }
    }

    void OnTriggerExit(Collider other)
    {
        if ((other.tag == "Player") && (doorState == DoorState.Opened) && (doorAnimationState == DoorAnimationState.None))
        {
            doorAnimationState = DoorAnimationState.Closing;
            doorState = DoorState.Closed;
            animating = true;
            if (reverse)
            {
                anim.SetTrigger("Close Door Reverse");
            }
            else
            {
                anim.SetTrigger("Close Door");
            }
        }
    }
}

Check out [this demo Unity package][1] that shows how it works (the player needs to be moved manually in the editor during game play - excuse the rough animations).

[77946-automaticdoor.zip|77946]

Use on Trigger Enter indeed. Check that your objects have a rigidbody, the player a normal collider and the door a trigger collider big as the “open range”
You can have 2 animations named “openLeft” “openRight”
and 2 animations named "closeLeft " “closeRight” or just handle that by scrpt with transform rotation.
You have to check if the player is on the door right or left when he enters the trigger, so
OnTriggerEnter (Collider other){
if (other.comparetag ("Player)){
If(other.transform.position.x<0)
// then he is left, and you can play the openright animation
{
anim.SetBool (“Left”,true);
}
Else{
anim.SetBool ("Left, false);
}
Anim.SetTrigger (“Open”);
//your door will start the animation based on the player side

}}

Then
OnTriggerExit (Collider other){
//if player compare tag etc, then
anim.SetTrigger (“Close”);
}

You need to do set the animator and reference it on the script
use a “Idle” animation that has nothing
Then, transiction 1 to “openRight” if left is false and if trigger “Open”
OpenLeft transiction if left is true and if trigger open

From openRight a transiction to closeright if trigger “close” and then transiction from closeright to idle
All your animation have dont have to be looped.

Sorry if i miss spelled something; like caps etc
I’m from phone and it’s 1.37 AM
Hope i helped you!
Write for more and i’ll explain ^^
There are lot of ways to do what you asked, keep in mind that