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
Answer by TBruce · Jul 15, 2016 at 07:24 PM
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;
}
}
@$$anonymous$$avina Thank you very much. I'm very happy. It works!!!! Cheers!
Jonah
Your answer
Follow this Question
Related Questions
Play more animations onclick 1 Answer
Move a object with a trigger? 0 Answers
Regarding game state within a scene 0 Answers
Text Will not disappear after Object Destroyed,Text Wont Go away after object disappears. 1 Answer
How can I collect collider2Ds(Trigger) the player is in into an array 1 Answer