You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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系统之后运行:

  1. 打开Project Settings → Script Execution Order
  2. 将你的脚本的执行顺序设置为晚于RigBuilder
  3. 同时确保Animation Rigging组件(如Two Bone IK Constraint)的Weight设为1,且启用了对应Foot IK的约束

关键注意事项

  • 不要同时混用Animator原生IK和Animation Rigging的IK,二者会互相干扰
  • 射线检测起点用animator.GetIKPosition获取当前帧脚部位置,避免依赖可能被修改的目标对象位置
  • 检查WalkableRayCast的射线长度、层过滤是否正确,确保能准确检测到地面

内容的提问来源于stack exchange,提问作者Yecats

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 12:18:12