来自于:吴雨葵
Unity OnAnimatorIK提供了简单IK的实现方式。
准备:
Model的Animation Type设置为Humanoid。
检测Avatar是否有异常。
Animator勾选IK Pass。
准备好之后,挂载的MonoBehaviour的OnAnimatorIK才会被回调。
代码:
public class IKTest : MonoBehaviour{[Range(0, 1)]// IK权重public float weight = 1;// IK跟随的物体public Transform rightHandFollowObj;public Transform leftHandFollowObj;protected Animator animator;private void Start(){animator = GetComponent<Animator>();}private void OnAnimatorIK(int layerIndex){if (animator){// 设置Position和Rotation权重animator.SetIKPositionWeight(AvatarIKGoal.RightHand, weight);animator.SetIKRotationWeight(AvatarIKGoal.RightHand, weight);animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, weight);animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, weight);// 改变Position和Rotationif (rightHandFollowObj != null){animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandFollowObj.position);animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandFollowObj.rotation);}if (leftHandFollowObj != null){animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandFollowObj.position);animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandFollowObj.rotation);}}}}

