首页 > 编程语言 > 详细

Unity3d 寻路功能 介绍及项目演示

时间:2015-03-07 22:44:29      阅读:312      评论:0      收藏:0      [点我收藏+]

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);
	}
	
	}
}

达到圆球朝鼠标点击的地方寻路效果

技术分享

Unity3d 寻路功能 介绍及项目演示

原文:http://blog.csdn.net/huutu/article/details/44102353

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!