[Unity3D插件系列]-A* Pathfinding Project 学习(一)

来源:互联网 发布:淘宝正规刷信誉平台 编辑:程序博客网 时间:2024/06/04 19:44

一直想在Demo中使用Unity3D自带的寻路模块,但是遇到一些问题:

        需求中要求游戏中的对象可以寻路的同时,成为其他对象的障碍,我就在同一个对象上添加了NavMeshAgent和NavMeshObstacle,但是出现了对象乱动的异常情况,查了官方文档还有论坛,没有对这种同时添加并起作用的情况有明确的解决方案;这时只能求助于其他寻路插件了,A* Pathfinding Project看起来还不错,考虑学习下。

1. 场景准备

        先建立一个scene

添加一个plane,让其坐标处于(0,0,0),并三方向scale都为10

        添加一个新的layer,命名为Ground,并将上面建立的plane设置为Ground层

        在plane上添加若干个box作为障碍物,添加一个新的layer,命名为Obstacles, 将这些box都归为这个Obstacles层

 2.加入A*

        创建一个空的GameObject,命名为A*, 从Components–>Pathfinding–>Pathfinder中添加插件脚本AstarPath。可以再AstarPath的观察器中看到它被分为几个部分,其中最重要的区域是Graphs区域和底部的Scan区域,Graphs区域保存了工程中所有的寻路图,最多可以有16个,但是一般1到2个已经足够了。有几类寻路图,其中最主要的有两种:Grid Pattern Graph和Navmesh Graph.

       这次就先添加Grid graph.

       就如名字所述一样,Grid graph会产生一系列的网格,大小为width * height,这个网格可以放在场景中的任何地方,也可以进行旋转。节点尺寸设置了节点所占空间的大小,在这里设置为1;右侧有一个5个点组成的小选取控制,选择左下角的那个点,将其坐标设置为(-50, 0.1, -50), 其中y方向设置为0.1是为了避免产生浮点错误,因为地面plane的y向坐标是0,如果导航网格也是y向为0的话,在进行高度检测的raycast的时候,会产生问题。

       

      高度测试:

      为了把寻路的node放置到场景中的正确位置,一般使用从node向下发射一个射线来进行检测,寻路node会被放置到碰撞点的位置。我们将mask设置为Ground,因为只希望寻路节点与Ground进行检测。

      碰撞测试:

      当寻路节点被放置之后,它就会被用来检测是否可行走,一般可以使用sphere,capsule或ray来进行碰撞检测。一般Capsule会使用和AI对象一样的半径和高度来进行碰撞。为了让AI对象和障碍物有一些边缘,这里将Capsule的半径设置为2.另外将碰撞检测的layer设置为Obstacles,因为不想让地面成为障碍。

      好了,都准备好了,点击底部的Scan,我们就可以看到grid Graph的生成了,可以再编辑窗口中看到辅助线显示的寻路网格,包括了可寻路的区域和障碍区域。

3. 加入AI

      以上是对场景寻路相关的基础设置,接下来要加入AI对象进行寻路。在场景里面添加一个Capsule,并给其添加一个Character Controller组件,从Components–>Pathfinding中添加Seeker脚本。Seeker脚本是一个帮助类的脚本,用来将其他脚本的寻路请求进行处理,它也可以处理Path modifier(一般是对寻路结果进行圆滑处理的脚本)。A* pathfinding project自带了两个AI脚本用于挂接到对象上进行寻路:AIPah可适用于任何类型的寻路图;而RichAI只适用于NavMesh类型。

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. //Note this line, if it is left out, the script won't know that the class 'Path' exists and it will throw compiler errors  
  4. //This line should always be present at the top of scripts which use pathfinding  
  5. using Pathfinding;  
  6. public class AstarAI : MonoBehaviour  
  7. {  
  8.     //The point to move to  
  9.     public Vector3 targetPosition;  
  10.   
  11.     private Seeker seeker;  
  12.     private CharacterController controller;  
  13.   
  14.     //The calculated path  
  15.     public Path path;  
  16.   
  17.     //The AI's speed per second  
  18.     public float speed = 200;  
  19.   
  20.     //The max distance from the AI to a waypoint for it to continue to the next waypoint  
  21.     public float nextWaypointDistance = 3;  
  22.   
  23.     //The waypoint we are currently moving towards  
  24.     private int currentWaypoint = 0;  
  25.   
  26.     public void Start()  
  27.     {  
  28.         seeker = GetComponent<Seeker>();  
  29.         controller = GetComponent<CharacterController>();  
  30.   
  31.         //Start a new path to the targetPosition, return the result to the OnPathComplete function  
  32.         //seeker.StartPath(transform.position, targetPosition, OnPathComplete);  
  33.     }  
  34.   
  35.     public void OnPathComplete(Path p)  
  36.     {  
  37.         Debug.Log("Yay, we got a path back. Did it have an error? " + p.error);  
  38.         if (!p.error)  
  39.         {  
  40.             path = p;  
  41.             //Reset the waypoint counter  
  42.             currentWaypoint = 0;  
  43.         }  
  44.     }  
  45.   
  46.     void Update()  
  47.     {  
  48.         // see if user pressed the mouse down  
  49.         if (Input.GetMouseButtonDown(0))  
  50.         {  
  51.             // We need to actually hit an object  
  52.             RaycastHit hit;  
  53.             if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))  
  54.                 return;  
  55.             // We need to hit something (with a collider on it)  
  56.             if (!hit.transform)  
  57.                 return;  
  58.   
  59.             // Get input vector from kayboard or analog stick and make it length 1 at most  
  60.             targetPosition = hit.point;  
  61.   
  62.             seeker.StartPath(transform.position, targetPosition);  
  63.         }  
  64.     }  
  65.   
  66.     public void FixedUpdate()  
  67.     {  
  68.         if (path == null)  
  69.         {  
  70.             //We have no path to move after yet  
  71.             return;  
  72.         }  
  73.   
  74.         if (currentWaypoint >= path.vectorPath.Count)  
  75.         {  
  76.             Debug.Log("End Of Path Reached");  
  77.             return;  
  78.         }  
  79.   
  80.         //Direction to the next waypoint  
  81.         Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;  
  82.         dir *= speed * Time.fixedDeltaTime;  
  83.         controller.SimpleMove(dir);  
  84.   
  85.         //Check if we are close enough to the next waypoint  
  86.         //If we are, proceed to follow the next waypoint  
  87.         if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance)  
  88.         {  
  89.             currentWaypoint++;  
  90.             return;  
  91.         }  
  92.     }  
  93. }  

这就是一个鼠标点击场景,然后AI对象进行寻路的脚本。如果觉得路线还不太平滑,可以在Components–>Pathfinding–>Modifier中添加寻路结果的修改脚本。

        好了,这个教程到此结束。

        结语:虽然按照这个教程做了一遍,但发现有些障碍会让AI对象停滞,过不去;但如果换成它自带的AIPah就没有这个问题。

0 0
原创粉丝点击