- Home /
Door Animation looping bug? (Beginner in Animation)
Good day everyone!
So I made a simple door opening and closing animation by rotating the door on the y axis, and I'm running into a bug.
The animation works with raycast and it opens or closes once the left mouse button is clicked.
I'm using two triggers (Open and Close) for my animations and I'm controlling them in my script.
The door opening animation works perfectly fine, however when I want to close the door, it sort of bugs out and loops 2-3 times before reaching the idle state. And what's even weirder is that it doesn't do this every time, but rather once in every 2 clicks. I'm not sure what I'm doing wrong.
PS: Loop Time is unchecked for both animations
Here is my code for the door animation:
public class MyDoorController : MonoBehaviour
{
private Animator doorAnim;
private bool doorOpen = false;
private void Awake()
{
doorAnim = gameObject.GetComponent<Animator>();
}
public void PlayAnimation()
{
if (!doorOpen)
{
doorOpen = true;
Debug.Log("door opening");
doorAnim.SetTrigger("Open");
}
else
{
doorOpen = false;
doorAnim.SetTrigger("Close");
}
}
}
Here's what my animator controller looks like:
The transition Idle -> DoorOpen has the Open trigger, and the transition DoorOpen -> DoorClose has the Close trigger.
What could I be doing wrong?
Answer by DevManuel · Feb 17, 2021 at 05:36 PM
Until now I hadn't done something with a trigger-var. I had used bool-vars. Maybe you try it out this way, too? I would remove the "close" and "open" trigger and just add an bool called "doorOpen", or something like this. Then the script would be like this:
public class MyDoorController : MonoBehaviour
{
private Animator doorAnim;
private bool doorOpen = false;
private void Awake()
{
doorAnim = gameObject.GetComponent<Animator>();
}
public void PlayAnimation()
{
doorOpen = !doorOpen;
Debug.Log("doorOpen = "+doorOpen);
doorAnim.SetBool("doorOpen", doorOpen);;
}
}
I'm not sure if it removes your iusse, but you can give it a try.
Hey there! I remember reading somewhere about how using triggers are more preferred if your animations are the type that don't usually loop. That's why I ended up using triggers as well. :D But I'll give this a try, and will let you know if it removes the problem!
Your answer
Follow this Question
Related Questions
How do I animate a character in unity with a humanoid skeleton? 0 Answers
Is there a new retargetting system for the animation in 5.5? 1 Answer
How can I make my guy stop running? 1 Answer
root motion on generic rig only works on a the base layer of an animator controller? 0 Answers
Animator for Props 0 Answers