- Home /
Cant get Animator Controller to work properly
So I have a shelf on a desk that slides in and out. Now when I press E on it, it first doesnt do anything, but on the second press it opens and closes right after.
So here is screenshot of my a-controller:
And here is the code that opens and closes it on button press:
void OnUse()
{
Debug.Log("IT WORKSSS");
if(anim.GetBool("IsClosed") == true)
{
anim.SetTrigger("OpenShelf");
anim.SetBool("IsClosed", false);
}
else
{
anim.SetTrigger("CloseShelf");
anim.SetBool("IsClosed", true);
}
Answer by kevojo · Sep 19, 2018 at 08:06 PM
I'm not sure why you have both two triggers and two bools for it. You could do this with only one trigger.
//Create a trigger called 'toggleShelfState' or something
if (Input.GetKey(keycode.E))
{
anim.setTrigger("ToggleShelfState");
}
As for setting up the transition - simply set one transition from and to. (Make the default state closed i guesss), uncheck "has exit time", and ensure they both have the same "toggleShelfState" condition. This should allow you to go back and forth between them no problem.
If you wanted to use a bool condition instead, you could do that.
//create a bool called "ShelfOpen" or something like that
if (Input.GetKey(keycode.E))
{
anim.setbool("ShelfOpen", !(anim.getbool("ShelfOpen")));
}
This will have essentially the same toggling effect - it will set the open state to the opposite of whatever it is when you press the button. Then just set transitions, ensure exit time is disabled, and set the condition for opening it to be ShelfOpen = true, and the condition for it closing is ShelfOpen = false
well without any modifications to my code, yes, although if you don't have an interruption source on the transition, it will finish before moving on to the next transition- however if you use the trigger option, it will queue up a trigger activation, so if you push E while it is transitioning to closed, it will immediately open again because it was triggered (these triggers don't stack). With the bool version, It will also wait to transition till the current transition is finished (if there is no interruption source), but it will not check whether it needs to transition or not till after it has finished - this means that if you spam the button, it will depend on whether you did it an even amount of times or an odd amount of times. If you want to prevent both of these, do this:
if (Input.Get$$anonymous$$ey(keycode.E))
{
if (!(anim.GetAnimatorTransitionInfo(0).IsName("Base Layer.Closed -> Base Layer.Open") || anim.GetAnimatorTransitionInfo(0).IsName("Base Layer.Open-> Base Layer.Closed")))
{
anim.setbool("ShelfOpen", !(anim.getbool("ShelfOpen")));
}
}
That is checking if either of those transitions are playing, and will return false if they are. That will only allow you to modify the bool (or set the trigger) after a transition has finished.
So I keep having this problem that it doesnt move from Closed to open? Here is my code:
public void Initialize()
{
anim = animObject.GetComponent<Animator>();
anim.SetBool("IsClosed", true);
}
// Update is called once per frame
void Update () {
}
void OnUse()
{
Debug.Log("IT WOR$$anonymous$$SSS");
if(anim.GetBool("IsClosed"))
{
if (!(anim.GetAnimatorTransitionInfo(0).IsName("Base Layer.Closed -> Base Layer.Open") || anim.GetAnimatorTransitionInfo(0).IsName("Base Layer.Open-> Base Layer.Closed")))
{
anim.SetBool("IsClosed", !(anim.GetBool("IsClosed")));
}
}
}
It just keeps looping at the "Closed" in this pic: http://prntscr.com/kweja5
Answer by UnityCoach · Sep 18, 2018 at 11:27 PM
To properly handle open/close, you actually need 4 states : close, opening, open, closing. You don't even need the trigger, the boolean value is enough. You can have a look at this video, in which I touch on the topic.
Hope this helps.
It went away but the animation does not play... Here is screenshot: http://prntscr.com/kwcl7v
You wouldn't need 4 states. If it's something simple like a shelf opening and closing, you can have 2 static poses, the shelf opened, and the shelf closed. The transition between the two will interpolate and you can control how many frames it takes for the speed.
I agree with you. It works with 2 states, but then you can close while it's still opening, and vice versa.
Only if it has an interruption source - otherwise (as I state it a different thread) it will wait till transition is finished, but it will still allow you to stack a closing/opening command while it is transitioning - so you need to check if it is in transition before taking the input.