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.
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.
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.
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.
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
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:
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.
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.
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?
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.