Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How to stop animation inside animator from going past 100%/0%?

Discussion in 'Animation' started by SimonOesterberg, Jul 22, 2018.

  1. SimonOesterberg

    SimonOesterberg

    Joined:
    Jul 17, 2018
    Posts:
    2


    I've looked online for an answer to this question for two days now but haven't been able to find anything so now I've decided to just ask the question myself.

    To explain what I'm trying to do and what I'm actually asking about. I'm trying to play a door opening animation that I've attached to a state inside of an animator. Then I'm trying to play it again in reverse when I want to close the door. It does work in the sense that the door opens when it should and that it starts the animation backward to close the door when it should.

    The problem is that the blue progress bar that you can see in the GIF seems to go to infinity so if I wait to close the door for, let's say, ten seconds after I've opened it I have to wait ten seconds before the progress bar reaches the end of the animation and the door closes. Is there a way for me to pause the bar from continuing to go left/right when it reaches the left and right edge? Or even better, is there a simpler way to play an animation forwards and backwards from a script?

    Script I use to close/open the door == the script that sets the animation speed to 1/-1 and plays it:

    Code (CSharp):
    1.     public void OpenDoor() {
    2.  
    3.         doorAnimator.SetFloat("Speed", 1.0f);
    4.         doorAnimator.Play("DoorOpen");
    5.         isOpen = true;
    6.  
    7.     }
    8.  
    9.     public void CloseDoor() {
    10.  
    11.         doorAnimator.SetFloat("Speed", -1.0f);
    12.         doorAnimator.Play("DoorOpen");
    13.         isOpen = false;
    14.        
    15.     }
     
  2. Goatogrammetry

    Goatogrammetry

    Joined:
    Apr 27, 2017
    Posts:
    197
    Wow. Kinda weird that it does that. I can see to a point whats going on in Unity and why... In the end though you're trying to force a real animation system to do something the wrong way. Here's what you need to do: Stop trying to make this work with 1 animation. Make a looping closed, looping open, and if you want to animate the open-close in a particular way, make those animations. Otherwise, use the transitions to animate the door opening and closing. This will use the animation system the way it was meant to and you wont have any problems.
     
    theANMATOR2b likes this.
  3. SimonOesterberg

    SimonOesterberg

    Joined:
    Jul 17, 2018
    Posts:
    2
    Ok so I'm not sure if I understand what you mean but I managed to solve it on my own. Thanks anyway for the reply!

    So what I did was that I added another state in the animator called DoorClose which plays the same animation as the DoorOpen except that its speed is set to -1 so that it plays the animation backwards. Then I just changed the script to be like this:

    Code (CSharp):
    1.     public void OpenDoor() {
    2.         doorAnimator.Play("DoorOpen");
    3.         isOpen = true;
    4.     }
    5.     public void CloseDoor() {
    6.         doorAnimator.Play("DoorClose");
    7.         isOpen = false;
    8.      
    9.     }
    The only problem that caused was that if I tried opening the door while it was closing, the animation would start from the beginning instead of just reversing direction. To fix this I added an if statement from this thread that makes it so that I can't open or close the door while an animation is playing, specifically:

    Code (CSharp):
    1.  if (doorAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !doorAnimator.IsInTransition(0))
    Now I can open the door and close it without any delay, the only thing different from what I had originally planned being that I have to wait until the animation has finished before I can open or close it again.
     
  4. Goatogrammetry

    Goatogrammetry

    Joined:
    Apr 27, 2017
    Posts:
    197
    You know you can do all of that functionality in a mechanim animator controller without a single bit of code. In the transitions there is a flag "Has Exit Time" that prevents interruption of the animation. I think you should probably spend a few hours learning about the existing systems and you'll save yourself a headache in the future when you're trying to re-invent the wheel. You essentially did what I suggested but instead of open and closed poses, you used the openING animation and just let it run past the end-- It works I guess, but when you load a level I bet you see half the doors suddenly close or open to get to their start states... Doors are easy and your code may work, but it only gets harder if you add a more anims and rules.
     
    theANMATOR2b likes this.