How do I Trigger my elevator to go up and then return back down?

I’m making a 2D game and I have a platform I want to use as an elevator. basically I want the platform to be static until the player walks/jumps on it and then stay at the finish point until the player leaves the platform then returns back to the starting point.

Currently the platform keeps repeating the “PLAYER_ON_ELEVATOR” animation and I’m not even on it to trigger the animation.

I have also tried this with the OnCollisionEnter/Exit2D method, but I get the same result.

Really appreciate any help.

public class TriggerLift : MonoBehaviour
{
    
    [SerializeField] float pause = 2f;

    //Tag string ref
    const string PLAYER_TAG = "Player";
    const string PLAYER_ON_PLATFORM_ANIM = "PlayerOnPlatform";
    const string PLAYER_OFF_PLATFORM_ANIM = "PlayerOffPlatform";

    Animator myAnimator;

    private void Awake()
    {
        myAnimator = GetComponent<Animator>();
    }


    private void OnTriggerEnter2D(UnityEngine.Collider2D col)
    {
        if (col.gameObject.CompareTag(PLAYER_TAG))
        {
            col.transform.SetParent(this.transform);
            myAnimator.SetTrigger(PLAYER_ON_PLATFORM_ANIM);
        }
    }

     private void OnTriggerExit2D(UnityEngine.Collider2D col)
     {
         if (col.gameObject.CompareTag(PLAYER_TAG))
         {
            col.transform.parent = null;
            StartCoroutine(PauseElevator());
            myAnimator.SetTrigger(PLAYER_OFF_PLATFORM_ANIM);
         }
     }

    IEnumerator PauseElevator()
    {
        yield return new WaitForSeconds(pause);
    }

}

Is your coroutine meant to wait for pause seconds? Because that’s now how things work.

  private void OnTriggerExit2D(UnityEngine.Collider2D col)
  {
      if (col.gameObject.CompareTag(PLAYER_TAG))
      {
         col.transform.parent = null;
         StartCoroutine(PauseElevator());
      }
  }

 IEnumerator PauseElevator()
 {
     yield return new WaitForSeconds(pause);
     myAnimator.SetTrigger(PLAYER_OFF_PLATFORM_ANIM);
 }

That’s how a coroutine works. StartCoroutine returns instantly, but the rest of the coroutine (after the yield return) is called later.