对于NavMeshAgent组件,如果直接用transform.position移动位置会报错如下:
xxx can only be called on an active agent that has been placed on a NavMesh。
需要使用如下方法进行移动位置,先不激活,移动完毕再激活。
using System.Collections;
using UnityEngine;
using UnityEngine.AI;public class NavAgentController:BaseObject
{private NavMeshAgent _agent;public NavMeshAgent Agent{get{if (_agent == null) _agent = GetComponent<NavMeshAgent>();return _agent;}}public void TransferTo(Vector3 position){StartCoroutine(TransferToIE(position));}IEnumerator TransferToIE(Vector3 position){Agent.enabled = false;Agent.transform.position = position;yield return new WaitForEndOfFrame();Agent.enabled = true;}
}