Я разрабатываю игру в теннис и теперь пытаюсь синхронизировать удар моего игрока при ударе по мячу. У меня пока только одна анимация, но этого достаточно для того, чего я сейчас пытаюсь достичь.
По сути, я хотел бы изменить стандартную анимацию во время выполнения, чтобы я мог уменьшить разрыв между ракеткой и мячом во время удара. Прямо сейчас я устанавливаю порог, что если мяч находится на определенном расстоянии от него, то это считается ударом. Этот порог слишком велик, чтобы он выглядел реалистично.
Я пробовал 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);
}
}
}