在我的场景中,我有一个移动到特定目的地的代理。当我尝试复制它以创建第二个代理时,结果行为非常奇怪:代理被传送到地图的相对角落并停止移动。此外,运行时的导航网格似乎发生了变化(如您在第二个屏幕截图中所见)。一旦我禁用一个代理,另一个代理就会按预期开始工作。
编辑:这是我用于角色的代码(如您所见,我手动管理角色旋转/翻译而不是委派代理,这在我将相同的脚本分配给前面提到的第二个角色之前效果很好)。
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;
}
}