How to give control back to animation in Unity3D once altering objects location

177 views Asked by At

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

There are 1 answers

0
Problematic On

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.