Search Unity

Somehow I can't get my transitions from walk to jump instantaneous.

Discussion in '2D' started by Th0masThe0bvious, Jan 30, 2019.

  1. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    I've been beating against this for a long time, doing just about everything that is usually recommended reducing the Transition duration down to zero, setting the interruption source to the next State, but somehow there is always still some leg wiggling when the character becomes airborn. What could be wrong here?
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    not enough information...
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    If you're using an animator controller, make sure the transition's Has Exit Time is unticked. Otherwise it won't transition until the walk animation has finished a full cycle.
     
  4. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    I'll give you any other information you want, on request.

    It is unticked.
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Keep the Animator window open on your character while playing in the editor. It should give you some insight into how the transition goes. If it happens too fast, temporarily set Time.timeScale to, say, 0.1 to make everything run 10 times slower. If you don't have a script that sets Time.timeScale back to 1, you can set this using Edit > Project Settings > Time.
     
  6. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    Thank you, but all that did was confirm that the character has started to move upwards before his jump animations start playing. I'm still not sure why.
     
    Last edited: Feb 1, 2019
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Sorry, I can't say. If you've confirmed that it's still playing the run animation and not in the process of transitioning when the character starts moving up, are you sure the transition is kicking off at the same time that you start moving the character up?
     
  8. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    In theory (which in this case, means "as far as I can see in the code and animation transition menu") yes. In practice, the movement still happens a bit before and I'm not sure why.
     
  9. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    Here is a discovery: Setting the jump transition as an "interruption" source for the walk transition has made it instantaneous, but then it snaps back to the walk frame in midair, albeit briefly. Please advise on how to fix this.
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Is there a transition from jump to walk? If so, make sure its conditions don't allow it to transition until the jump is done.
     
  11. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    No, but there is a transition from "Any State" to Walk. And another to Jump. Maybe that's not a good idea; I'm not sure.
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Transitions from Any State always have a condition. Make sure the condition from Any State to Walk isn't true during the jump.
     
  13. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    Sounds like a good idea, but how do I make that not true?
     
  14. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Click on the Any State -> Walk transition arrow. What's the condition?

    Let's say the Any State -> Jump transition's condition checks if a Boolean animator parameter named "Jumping" is true, and the transition is configured so it can't re-transition to itself. We'll assume in your character control script you set "Jumping" true while the character is jumping, and false otherwise.

    Say also that Any State -> Walk's condition checks if a float parameter "Speed" is > 0.01.

    Just add a second condition to this transition that "Jumping" must be false.
     
  15. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    I am unable to check for speed directly in the animator because my game utilizes moving platforms; the character has to be able to stand still on them while still riding. Here is my current movement code:

    Code (CSharp):
    1. float direction = (Input.GetAxis("Horizontal"));
    2.  
    3.         PlayerRigidBody.velocity = new Vector2(direction*2 + platformXVelocity, PlayerRigidBody.velocity.y);
    Here, therefor, I make walking a bool based on just the direction:

    Code (CSharp):
    1.  if (Mathf.Abs(direction) > 0 && onGround)
    2.         {
    3.             PlayerAnimations.SetBool ("Walking", true);
    4.         }
    5.         else
    6.         {
    7.             PlayerAnimations.SetBool("Walking", false);
    8.         }
    Finally, I had tried using the jumping bool and test if it wasn't true in order to regulate the walk animation, but no luck. Or rather some luck; the character won't start walking in midair if I press a direction while jumping, but if he jumps while walking on the ground, he will keep walking in air briefly. For completion's sake, here is the code for the jump:

    Code (CSharp):
    1.  yVelocity = PlayerRigidBody.velocity.y;
    2.  
    3.         if (Input.GetButtonDown("Jump") && onGround)
    4.         {
    5.             Jump();
    6.         }
    7. if ((yVelocity > 0) && gliding == false && onGround == false)
    8.         {
    9.             PlayerAnimations.SetBool("JumpUp", true);
    10.         }
    11.  void Jump()
    12.     {  
    13.         Vector3 velocity = PlayerRigidBody.velocity;
    14.         velocity.y = jumpPower;
    15.         PlayerRigidBody.velocity = velocity;
    16.     }
    17.  
    Note that "JumpUp" Bool. You might think that it would be better to set it with a button press than just trajectory. The thing is, I tried that; the button press can't seem to set a bool to true, so I switched it to a Jump Trigger, which didn't fix the problem. I went on to ruin that version of the game irreparably, and have reverted to an older one to start from here.

    I will share any other code you need. Something is definitely wrong with the walking animation, but I can't tell what.
     
  16. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    You might want to compare your animator controller to the one in Unity's standard assets (RobotBoy). It might give you some ideas on adjusting your transitions.
     
  17. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    Possibly, but Robotboy is 3D, right? I haven't really worked much with 3D but read that it handles animations quite differently.
     
  18. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    It's 2D.
     
  19. DSSirius

    DSSirius

    Joined:
    Mar 15, 2017
    Posts:
    40
    You may want to recheck your platform engine code because I equally have moving platforms in my engine and it has not hindered me in having the animator check speed at all.

    Do not have your player movement script check for moving platforms, instead, get your moving platforms to check for collisions from characters above, latch said characters to the platform and adjust the characters movement based on the platform movement.
     
  20. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    Sounds like a good idea, but do you have any code for that? Also note that I want my character to maintain momentum from a moving platform if he jumps while riding it, so it won't leave him behind.