我正在开发网球比赛,现在我试图在击球时同步球员的挥杆动作。目前我只有一个动画,但是对于我目前想要实现的动画来说已经足够了。
基本上,我想在运行时更改默认动画,以便在撞击时减小球拍和球之间的间隙。现在,我正在设置一个阈值,如果球在一定距离内,则视为击中。该阈值太大,无法使其看起来逼真。
我尝试IK效果不佳。我不知道在这一点上IK是否真的可行,因为如果可能的话,它似乎需要更多的计算和复杂性。我想知道是否有更简单的方法可以尝试。
//a callback for calculating IK
void OnAnimatorIK() {
if (animator) {
//if the IK is active, set the position and rotation directly to the goal.
if (ikActive) {
// Set the right hand target position and rotation, if one has been assigned
if (ball != null) {
Vector3 currentPosition = animator.GetIKPosition(AvatarIKGoal.RightHand);
//The avatar the IK is running on
GameObject avatar = this.gameObject;
//Distance from avatar to ball
float distanceToBall = Vector3.Distance(racket.transform.position, ball.position);
//Weight will be close to 1 as ball gets closer to the avatar
float weight = 1 - Mathf.Max(Mathf.Min(distanceToBall / 50, 1), 0);
//Set IK target weight
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, weight);
//Set IK target
animator.SetIKPosition(AvatarIKGoal.RightHand, new Vector3(currentPosition.x + 10, currentPosition.y, currentPosition.z));
}
}
//if the IK is not active, set the position and rotation of the hand and head back to the original position
else {
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
}
}
}