1.取出游戏物体上的NavMesh Agent组件
2.SetDestination(Vector3 v)
案例:
点击区域中的任一点,实现player的寻路导航。(不用寻路,如何实现点击某点,移动player到该处?)
private NavMeshAgent agent; void Start(){ agent = GetComponent<NavMeshAgent>(); } void Update(){ if(Input.GetMouseButtonDown(0)){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit)){ //需要添加该点是否是有效目标点的判断 agent.SetDestination(hit.point); } } }
试一试:
寻路能否穿过非静态、带碰撞体的游戏物体?