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

时间:2015-06-15 15:16:50

标签: animation unity3d unityscript

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 个答案:

答案 0 :(得分:0)

您想使用inverse kinematics并让Unity为您确定定位的工作。这是一个快速而肮脏的(未经测试的)捕捉球的例子(它在C#中,但对于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?)
    }
}

您需要做一些工作(可能是光线投射或只检查距离和方向)以确定何时将isCatching标志设置为true,并且您将要使用{{3的权重}和position让它看起来很自然。 rotation有更详细的信息。