欢迎大家来到我的博客http://unity.gopedu.com/home.php?mod=space&uid=3352&do=blog&view=me&from=space
Unity学习之自动寻径
自动寻径大体可分为8步
1.创建地形
2.添加角色
3.创建多个障碍物,尽量摆的复杂一点,来检查Navmesh
的可用性和效率。
4.选中地形,在Navigation窗口中,设置Navigation Static
5.依次选中障碍物,在avigation窗口中,设置Navigation Static
6.Navigation窗口中,选择Bake(烘焙)界面,点击Bake按钮,进程场景烘焙,就可以烘焙出寻路网格了
7.为角色添加NavMeshAgent组件。Component->Navigation->Nav Mesh Agent
8.为角色新增一个脚本PlayerController.cs,实现点击目标,自动寻路功能
巨魔自动寻径的代码
• private NavMeshAgent agent;
• private Animator anim;
• void Start() {
• agent = GetComponent<NavMeshAgent>();
• anim = GetComponent<Animator>();
• }
• void Update() {
• RaycastHit hit;
• if (Input.GetMouseButtonDown(0)) {
• Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
• if (Physics.Raycast(ray, out hit)){
• agent.SetDestination(hit.point);
• }
}
}
请继续关注我的博客
http://unity.gopedu.com/home.php?mod=space&uid=3352&do=blog&view=me&from=space
更多精彩尽在http://www.gopedu.com/