Unity3d中的寻路,可以使用AStarPath 寻路插件。现在也可以使用Unity自带的 Navigation 功能来做。
来做一个例子:
上面的图片中,Cube 是阻碍物体,球 是代表玩家,要寻路。
设置Cube为不可通过物体
首先我们点击Window - Navigation 窗口,然后选中4个Cube,按照下图设置这4个Cube为不可通过,然后烘培
设置地面为可通过,然后烘培
我们给圆球也就是我们的主角加上控制脚本
using UnityEngine;
using System.Collections;
public class findpath : MonoBehaviour {
public NavMeshAgent agent;
Vector3 point;
Ray aray;
RaycastHit ahit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
aray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(aray,out ahit))
{
point = ahit.point;
}
agent.SetDestination(point);
}
}
}
原文地址:http://blog.csdn.net/huutu/article/details/44102353