【问题标题】:Unity3D Duplicated NavMesh Agents not movingUnity3D 复制的 NavMesh 代理不移动
【发布时间】:2019-05-05 20:00:33
【问题描述】:

在我的场景中,我有一个移动到特定目的地的代理。当我尝试复制它以创建第二个代理时,结果行为非常奇怪:代理被传送到地图的相对角落并停止移动。 此外,运行时的导航网格似乎发生了变化(如您在第二个屏幕截图中所见)。一旦我禁用一个代理,另一个代理就会按预期开始工作。

编辑: 这是我用于角色的代码(如您所见,我手动管理角色旋转/翻译而不是委托代理,这在我将相同的脚本分配给前面提到的第二个角色之前效果很好)。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AI_PlayerController : MonoBehaviour
{
    public Transform goal;
    bool isTracking = false;
    public float speed = 1.0f;
    Vector3 currentDirection;

    Animator anim;    
    NavMeshAgent agent;
    Vector2 smoothDeltaPosition = Vector2.zero;
    Vector2 velocity = Vector2.zero;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(goal.position);
        agent.updatePosition = false;
        agent.updateRotation = false;
    }
void Update ()
    {
        if (agent.remainingDistance > agent.radius) {
            Vector3 worldDeltaPosition = agent.nextPosition - transform.position;        
            worldDeltaPosition.y = 0;
            Quaternion rotation = Quaternion.LookRotation(worldDeltaPosition);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, .1f);
            anim.SetBool("move", true);
        } else {
            anim.SetBool("move", false);
        }

    }

    void OnAnimatorMove ()
    {
        // Update position to agent position
        transform.position = agent.nextPosition;
    }
}

【问题讨论】:

    标签: unity3d navmesh navagent


    【解决方案1】:

    您是否清除并重新烘焙了导航网格?我不确定这是否可行,但它可能会解决问题。否则,请尝试在两者上删除并重新添加 NavMeshAgent 组件。如果它们是预制件,那么您可能需要重新创建它们而不是复制它们,这样不需要的更改就不会在它们之间意外传播。

    编辑:不看代码就很难确定问题。你能上传导航/移动脚本吗?

    编辑 2 您可以尝试用以下移动脚本替换两个角色的脚本吗?动画逻辑可以稍后再添加。

    如果这不起作用,那么我建议检查您的场景中是否正确设置了导航网格(例如,确保正确标记导航障碍,以及清除和重新烘焙导航网格)。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    
    [RequireComponent(typeof(NavMeshAgent))]
    public class AI_PlayerController : MonoBehaviour
    {
        public Transform goal;
        public float speed = 3.0f;
        public float acceleration = 8.0f;
    
        NavMeshAgent agent;
    
        // Start is called before the first frame update
        void Start()
        {
            agent = GetComponent<NavMeshAgent>();
            agent.speed = speed;
            agent.acceleration = acceleration;
            agent.SetDestination(goal.position);
        }
    }
    

    【讨论】:

    • 是的,我尝试重新烘焙并删除/添加 NavMeshAgent,但没有任何改变。我已将源代码添加到问题中。谢谢
    • 好的,我会试试这个变体,让你知道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多