Search Unity

Hoping for help and guidance with a JUMP animation.

Discussion in 'Animation' started by alexchesser, Apr 22, 2018.

  1. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    39
    hi everyone!

    Over the weekend I watched this masterpiece of an animation tutorial and it was phenomenal. I went from having the daunting task of implementing dozens of actions and run directions to ... complete comfort and understanding of at least one slice of character motion.

    Additionally, the web of possible interconnected states just feels so much more manageable using blend tree methods.



    Now I'm trying to figure out how to add JUMP and JETPACK actions to my controller.

    I Have both actions as animations available to me, but they have no ROOT MOTION within them so if my 'toon is running in any given direction, with a tap of the "jump" button, they'll stop dead in place and execute the vertical animation motion.

    What I'd hope for in case of jump is that motion would continue in the current direction and speed over the course of the jump (or whatever feels like fun after I play with it)

    I've tried a few things, but nothing has worked so far. I'm wondering if anyone has a link to a solid tutorial on how to write an animated jump for unity or can give a run down of the things they've personally done in the past.

    As crazy as it sounds, I'm writing this post from vacation, so I can't actually try anything out until I get home, but I can't get this problem out of my head, so I'm hoping to come home with a few strategies to try at my fingertips.
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,780
    What are the few things you have tried?
    Are you talking about writing code?
    This is expected behavior based on the info you have provided.

    You have 3 options available to you.
    1. Edit (or have someone) edit the existing animations you currently have (in 3D package) to generate the root motion data.
    2. Write (or have someone) write the code that will serve as the motion for the character during the jump and jetpack animations.
    3. Generate root motion in Unity. Note - I do not know how effective this process is with a fully rigged character/hierarchy.
    https://unity3d.com/learn/tutorials/topics/animation/authoring-root-motion?playlist=17099
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,293
    Hey you can’t just say you saw the best animation tutorial in the history of time and not leave a link :eek:
     
    BrandyStarbrite likes this.
  4. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    39
    Haha Petey - it's that "setting up a humanoid avatar" video embedded in the post. Using the blend tree you're able to get all directional movements working without micro-managing transitions and conditions for each possible state transition.
     
  5. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    39

    Thanks for the reply!

    I should have mentioned that yes I'm talking about writing code. I'm still writing this from a beach vacation so I don't have my exact code. But the farthest I got in the process of writing the code was adding a Boolean "IsJumping" which triggers if the user has pressed the space bar via a custom StateMachineBehavior. That will initiate the transition from my blend-tree of directional movement to the Jump animation.

    The "code thing" I've tried is to apply an impulse to the animated model's rigidbody... but the effect of that is the character appears to "teleport forward" and then stop and jump.

    I'm trying to figure out if there is a tutorial or example of code somewhere in which a no-root-motion-animation is given an additional transform through the frames.
     
  6. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    855
    probably you'll have to cut your jump animation (depending on the animation) into three parts: jumpoff, in-air and landing.

    then you apply force to the capsule and play these 3 parts based on the height from ground.
     
  7. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    39
    That's interesting since the JETPACK animation already comes with a three-part animation as well as a fully-connected one. So that sounds like you might be on the right track. Additionally, I saw in the video above that making sub-clips of larger animation sets is *relatively* simple.

    Have you seen any examples of the force calculation done anywhere else in a place I might inspect the code? I'm trying to figure out if I might have to do this in a separate jump-component script which is unrelated to the StateMachineBehavior script.

    Thanks for the reply!
     
  8. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    Hi, I don't suppose you have tried making the player character a child object of an empty object and then just move the empty parent object in code? That way you can just play the animations and do all the movement separately, just a thought
     
  9. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    39
    Finally back from vacation - I spent some time with this last night.

    Discovered that the issue with being unable to apply force through an animation is related to the root motion issue.

    When I added:

    Code (CSharp):
    1. animator.applyRootMotion = false;
    My jump was able to feel really "jumpy" when I added that. This added a host of other problems, but for the moment I think many of them are probably and at the very least more appropriate in a new thread.
     
    theANMATOR2b likes this.
  10. Automoda

    Automoda

    Joined:
    Apr 27, 2017
    Posts:
    137
    I just switch from relying on root motion to applying velocity when jump is happening. And a 3 part animation with no root motion. Once he hits the ground I kill all velocity.
     
    theANMATOR2b likes this.
  11. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    39
    Thanks for the reply!

    Is your three part animation a Blend tree?
    How do you decide when to switch from START - IN_AIR - LANDING? I tried looking at the standard assets character animation set but they don't have 3 part animations.

    Do you detect ground collision with a Physics raycast or do you have some other technique?
     
  12. Automoda

    Automoda

    Joined:
    Apr 27, 2017
    Posts:
    137
    Pardon my code-- I'm an animator, not a programmer.

    jump.gif


    Skip to about 1:30 and there's some crappy footage of the running version of this jump. Its just some movement tests so its not too cinematic.


    // -----------JUMP---------------
    // Actual upward velocity added via the "JumpStart" animation event
    if (m_controller.isGrounded)
    {
    m_animator.SetBool("OnGround", true);
    groundedLastFrame = true;
    verticalVelocity = -gravity * Time.deltaTime;
    fallingMomentum = 0.0f;

    if (buttonJ && jumpCoolDown == 0.0f) // If jump button is pressed and we haven't jumped for a few seconds...
    {
    m_animator.SetBool("JumpTrigger", true); // Move to jump animation
    jumpCoolDown = 3.0f; // No more jumping for 3 seconds
    }
    }
    else
    {
    verticalVelocity -= gravity * Time.deltaTime;
    m_animator.SetBool("OnGround", false);
    groundedLastFrame = false;
    }
    jumpCoolDown = Mathf.MoveTowards(jumpCoolDown, 0.0f, 1.0f * Time.deltaTime); //Move jumpCoolDown toward 0 over time

    And here's the part that happens when the jump event is detected....


    // Creates the upward force for jumping
    // Called by an animation event to allow for preparatory animation
    private void JumpStart ()
    {
    verticalVelocity = jumpForce;
    if (runAmount > 0.8f)
    fallingMomentum = 10.0f;
    else
    fallingMomentum = 3.0f + (speed * 0.5f);
    }

    I think somewhere I set a forward velocity... I'd have to dig around for that. It took me all day to figure out how to set the velocity to monster-local forward.
     
    theANMATOR2b and alexchesser like this.