Unity - 简单的IK实现-OnAnimatorIK
程序员文章站
2024-02-22 16:12:28
...
Unity - 简单的IK实现-OnAnimatorIK
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和Rotation
if (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);
}
}
}
}
效果:
上一篇: 爬虫相关总结