《Unity3D-自动寻路功能的实现》

//思路:自动寻路是根据场景中NavMeshAgent的功能,来自于AI的引用集。利用渲染以后的目标的位置设置来跟踪目标的位置

//注意点:此方法中旧的stop和resume方法已经弃用了

代码:

// 1.设置追踪点

private NavMeshAgent agent;

private Transform player;   //2.设置目标点

void Awake(){

  agent=this.GetComponent<NavMeshAgent>();  //3.初始化追踪点

}

void Start(){

  player=GameObject.FindGameObjectWithTag("Player").transform;  //4.初始化目标点

}

void Updata(){

  if(Vector3.Distance(transform.position,palyer.position)<3f){  //6.判断如果目标点的位置小鱼2f的时候

  agent.isStop=true;  //6.1追踪点停止追踪

}else{

  agent,isStop=false;  //6.2追踪点开始追踪

  agent.SetDestination(palyer.position);  //5.设置追踪点的目标位置

}

}

原文地址:https://www.cnblogs.com/ylllove/p/7266006.html