Unity技术求助:使用Root Motion时角色有动画但不随Nav Agent移动
以下是针对该问题的常见排查和解决步骤:
同步Animator与Nav Agent的位移逻辑
同时使用Root Motion和Nav Mesh Agent时,需通过代码协调两者控制权,避免冲突。在挂载Animator的脚本中添加OnAnimatorMove()方法同步位置与旋转:private NavMeshAgent navAgent; private Animator animator; void Awake() { navAgent = GetComponent<NavMeshAgent>(); animator = GetComponent<Animator>(); // 初始关闭Nav自动更新,由Root Motion或代码控制 navAgent.updatePosition = false; navAgent.updateRotation = false; } void OnAnimatorMove() { if (!navAgent.pathPending && navAgent.remainingDistance > navAgent.stoppingDistance) { // Nav有有效路径时,让角色向目标位置过渡,保留Root Motion动画自然感 transform.position = Vector3.Lerp(transform.position, navAgent.nextPosition, animator.deltaTime); transform.rotation = Quaternion.Lerp(transform.rotation, navAgent.desiredRotation, animator.deltaTime * 6f); } else { // 无路径时完全由Root Motion控制位移旋转,同步Nav位置 transform.position += animator.deltaPosition; transform.rotation *= animator.deltaRotation; navAgent.nextPosition = transform.position; } }检查Animator组件设置
- 确认Animator组件的Apply Root Motion处于勾选状态,保证动画Root Motion数据能作用于角色物体;
- 若角色模型为子物体,需将Animator挂载在带有Nav Mesh Agent的父物体上,或通过脚本将Root Motion位移传递给父物体。
验证动画剪辑的Root Motion配置
打开移动动画剪辑的Inspector面板:- 勾选Root Motion选项;
- 将Root Transform Position (XZ)设为Based on Root Motion,确保水平位移数据被正确提取;
- 将Root Transform Rotation设为Based on Root Motion,保证旋转数据正常生效。
调整Nav Mesh Agent参数
- 确保Nav Agent的Speed与动画Root Motion移动速度匹配,避免动画播放速度和Nav路径规划速度不一致;
- 若完全依赖Root Motion驱动移动,保持Update Position和Update Rotation关闭,由代码同步Nav Agent位置信息。
内容的提问来源于stack exchange,提问作者ali mahdi
相关产品推荐
相关产品推荐

