1

I have a character made of up child objects that are animated using Unity3D's animation system.

While the player is walking, I can programmatically move the hand object up to catch a ball using the following code.

hand.position.y = ball.transform.position.y;

I need the hand object to go back to following the walk animation after it touches the ball, but instead it just stays at the exact position since it was set.

1 Answer 1

0

You want to use inverse kinematics and let Unity do the work of figuring out positioning for you. Here's a quick-and-dirty (untested) example for catching a ball (it's in C#, but it should be pretty similar for UnityScript):

// in a script on the same GameObject as your animation controller
bool isCatching;
Transform ball;

void OnAnimatorIK (int layer) {
    if (isCatching) {
        // set position and rotation weights for your catching hand
        animator.SetIKPosition(AvatarIKGoal.RightHand, ball.position);
        animator.SetIKRotation(AvatarIKGoal.RightHand, ball.rotation);
    } else {
        // return your position and rotation weights back to their defaults (probably 0f?)
    }
}

You'll need to do some work (possibly raycasting or just checking distance and direction) to determine when to set the isCatching flag to true, and you'll want to play with the weights for position and rotation to make it look natural. The IK manual entry has more detailed information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.