John Smith
John Smith

Reputation: 75

Unity Open/Close doors with bool paramaters

I'm new to Unity and i'm creating some project. I've already made open and close animation, disabled loop time, created bool parameter "open" and put conditions:

Empty -> openDoor (open = true)
openDoor -> closeDoor (open = false)
closeDoor -> openDoor (open = true)

With left click i have to open and close the door. This is the C# code, I tried playing with if states but I can't get it work. Any help?

void Update()
{
    if (Input.GetMouseButton(0))
    {
        anim.SetBool("open", true);

        if (anim.GetBool("open") == true)
        {
            anim.SetBool("open", false);
        }
    }
}

Upvotes: 1

Views: 795

Answers (1)

Mehrdad Dowlatabadi
Mehrdad Dowlatabadi

Reputation: 1335

If you mean you want to change door status by clicking, you can negate(not) the bool like this:

void Update()
{
   if (Input.GetMouseButton(0))
   {
      anim.SetBool("open", !(anim.GetBool("open")));

   }
}

and if you need to close/open door by clicking on it you can use OnMouseDown method:

 void OnMouseDown(){
  anim.SetBool("open", !(anim.GetBool("open")));
 }   

Upvotes: 1

Related Questions