0

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

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

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;
    }
}

只有一种活性剂 两个重复的代理都处于活动状态

4

1 回答 1

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);
    }
}
于 2019-05-07T17:17:26.860 回答