Friday, July 17, 2015

unity - Jumping with Mecanim synchronization


I am using Unity3D 4.1 for one of my projects. I have a robot character which is always running. I am using mecanim animation system.





  • What I really want:When I press Space bar, the character should jump up in the air, triggering an animation clip and then by the time it reaches the ground, the animation clip should also end.




  • What actually is happening:When I press Space bar, the character jumps in the air. Animation clip plays as it should, but ends way before it reaches the ground. So, it looks like he is running in the mid air.




  • What have I done: I have this humanoid robot setup with a jump animation bounded with the space bar key. Also, instead of using root motion, I am directly moving the robot from code.


        //Jumping
    if(Input.GetKeyDown(KeyCode.Space)){

    rigidbody.AddForce(Vector3.up*jumpVelocty);
    anim.SetBool("Jump",true);

    }
    else
    anim.SetBool("Jump",false);


Character's Details:




  1. Rigidbody = Mass:30, Freeze rotaion:x,y,z

  2. Capsule Collider = Material: metal, center(0,4.5,0), radius:1, height:11

  3. Script = jumpVelocity:20000


Jump Animation Clip: ~ 2 seconds. I am really out of ideas how to synchronize everything. Should I make the character jump in some other way so that it quickly comes down and touches the ground to match the animation clip? If yes, please provide a direction.



Answer



You need to use multiple clips for this. Because you're using a single clip, and your jump is handled via physics, you can't really (easily) figure out the time the animation should take to complete, and even if you went that route, you'd have to slow your animation down to match.


What you need to do is split the animation into 3 separate clips, a jump, an in-air loop, and a landing animation.


You'd play the jump animation when you hit the jump button.


Transition into your in-air loop and remain there as long as the character is not grounded,



then when the character becomes grounded, transition to your landing animation.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...