Using Mecanim Trigger with same Key

Hi,

I’m trying to get a very simple Door opening/closing with Mecanim. I’ve done this other ways before but I wanted to try get using Mecanim more.

What I’m trying to do seems so simple to me but I really can’t figure it out at all, I’ve looked through previous answers and googled several different tutorials but they all seem to do the same thing, which is open a door with a key press, then the door closes one they exit the trigger area.

However, what I want to do it open the door with “E” for example. Then when the mecanim triggers to open the door, and the door is now “open”, the next time I press “E” it will close it.

I have 3 animations; Idle, Open, Closed. I have 2 triggers in Mecanim; Open, Close.

So what I want to do is the first time I press “E”, i set the Open trigger. Then the 2nd time I press E its sets the Close trigger…

Here’s the code if that helps people see exactly what I mean:

public class OpenDoor : MonoBehaviour
{
Animator anim;
bool inRange = false;

void Start()
{
    anim = GetComponent<Animator>();
}

void Update()
{
    if (inRange)
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            anim.SetTrigger("Open");
        }

        // This is where I'm having trouble.. 
        // I have another anim.SetTrigger("Close") that I also want to
        // use with the E input, but I can't figure out how to use it..
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        inRange = true;
    }
}
void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        inRange = false;
    }
}

}

Make a simple switch to check door’s current state and set parameter according to it.

void Update()
     {
         if (inRange)
         {
             if (Input.GetKeyDown(KeyCode.E) && door)
             {
                 anim.SetTrigger("Open");
                 door = !door;
             }
             
             if (Input.GetKeyDown(KeyCode.E) && !door)
             {
                 anim.SetTrigger("Close");
                 door = !door;
             }
    
             
         }
     }