Open and close a dooor

Hi everybody,

in my project I have a door that opens when I mouseclick on it. This works. Now I want it to close when I click it again. For that I want to use the code ‘anim.SetTrigger(“deurdicht”);’’

I think it should be and if/else statement but I can’t figure it out. This is the code I have so far.

using UnityEngine;
using System.Collections;

public class ScriptLoop : MonoBehaviour
{

    public Animator anim; //anim kan ook een andre naam zijn, is willekurig

    

    // Use this for initialization
    void Start()
    {

    }

    
    void Update()
    {

    }
    void OnMouseDown()
    {
        anim.SetTrigger("deuropen");
    }
  }

I hope that someone can help me out. Thanks in advance!

Cheers,

Jonah

The script below assumes that you have a trigger called “deurclose” in your animator that playse the door close animation

using UnityEngine;
using System.Collections;

public class ScriptLoop : MonoBehaviour
{
    public Animator anim; //anim kan ook een andre naam zijn, is willekurig

    private bool deurOpened = false;

    // Use this for initialization
    void Start()
    {

    }

    
    void Update()
    {

    }

    void OnMouseDown()
    {
        if (!deurOpened)
        {
            anim.SetTrigger("deuropen");
        }
        else
        {
            anim.SetTrigger("deurclose");
        }
        deurOpened = !deurOpened;
    }
}