Animation Rig Package结合OnAnimatorIK实现FootIK遇覆盖问题求助
FootIK 脚部对齐失效问题解决
核心原因
你直接修改IK目标Transform的做法错误,这些设置会被Animator动画更新流程或Animation Rigging系统的后续计算覆盖。Unity的动画管线执行顺序是:Animator计算当前帧动画 → OnAnimatorIK调用 → Animation Rigging更新,所以直接改Transform的操作会被后续步骤冲掉;移到LateUpdate只能在帧末看到效果,是因为动画更新已经完成,下一帧又会被覆盖。
正确实现方案
方案1:使用Animator原生IK API(推荐)
放弃修改IK目标对象的Transform,直接在OnAnimatorIK中调用Animator的IK方法,这些设置会直接作用于Animator的动画计算,不会被覆盖:
private Animator animator; private void Awake() { animator = GetComponent<Animator>(); } private void OnAnimatorIK(int layerIndex) { // 完全启用双脚IK权重 animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1f); animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1f); animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1f); animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1f); // 右脚射线检测:基于Animator当前脚部位置向上偏移 Vector3 rightFootRayOrigin = animator.GetIKPosition(AvatarIKGoal.RightFoot) + Vector3.up; RaycastHit rightFootGroundPoint = WalkableRayCast(rightFootRayOrigin); if (rightFootGroundPoint.point != Vector3.zero) { // 设置脚部位置(添加离地高度) Vector3 newFootPos = rightFootGroundPoint.point; newFootPos.y += _footDistanceToGround; animator.SetIKPosition(AvatarIKGoal.RightFoot, newFootPos); // 设置脚部旋转:对齐地面法线,朝向角色前方 Quaternion footRot = Quaternion.LookRotation(transform.forward, rightFootGroundPoint.normal); animator.SetIKRotation(AvatarIKGoal.RightFoot, footRot); } // 左脚射线检测逻辑同理 Vector3 leftFootRayOrigin = animator.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up; RaycastHit leftFootGroundPoint = WalkableRayCast(leftFootRayOrigin); if (leftFootGroundPoint.point != Vector3.zero) { Vector3 newFootPos = leftFootGroundPoint.point; newFootPos.y += _footDistanceToGround; animator.SetIKPosition(AvatarIKGoal.LeftFoot, newFootPos); Quaternion footRot = Quaternion.LookRotation(transform.forward, leftFootGroundPoint.normal); animator.SetIKRotation(AvatarIKGoal.LeftFoot, footRot); } }
方案2:适配Animation Rigging的目标对象方式
如果必须用_rightFootTarget这类Transform作为IK目标,需要调整执行顺序确保你的代码在Rig系统之后运行:
- 打开
Project Settings → Script Execution Order - 将你的脚本的执行顺序设置为晚于
RigBuilder - 同时确保Animation Rigging组件(如Two Bone IK Constraint)的
Weight设为1,且启用了对应Foot IK的约束
关键注意事项
- 不要同时混用Animator原生IK和Animation Rigging的IK,二者会互相干扰
- 射线检测起点用
animator.GetIKPosition获取当前帧脚部位置,避免依赖可能被修改的目标对象位置 - 检查
WalkableRayCast的射线长度、层过滤是否正确,确保能准确检测到地面
内容的提问来源于stack exchange,提问作者Yecats
相关产品推荐
相关产品推荐

