学了一段时间的寻路,在网上也学了挺多算法,今天整理了一下,使用到Unity的3D界面中用于寻路,首先是简单的寻路算法,以下是地图:
地图数组:
using UnityEngine;
using System.Collections;
public static class MapsArray {
public static int[,] MazeItem = new int[15, 10] //初始化迷宫
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,0,0,1,1},
{1,0,0,1,1,0,1,0,1,1},
{1,0,0,0,0,0,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,1,0,1,0,0,0,0,1,1},
{1,0,0,0,1,1,1,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,0,1,1,1,0,0,0,1},
{1,1,0,0,1,1,1,0,1,1},
{1,1,1,0,0,0,0,0,1,1},
{1,1,1,1,0,0,1,0,1,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
}
然后是简单寻路的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestPathing : MonoBehaviour {
private int[,] MazeItem; //初始化迷宫
private GameObject npc; // npc
private List<Vector3> path; // 路径
private Vector3 target = Vector3.zero;
private float speed = 4; // npc移动速度
private int n = 0; // 当前已经移动的路点
private const int xStart = 1;
private const int yStart = 1;
private const int xEnd = 8;
private const int yEnd = 8;
void Start () {
MazeItem = MapsArray.MazeItem; // 初始化迷宫数组
path = new List<Vector3> ();
StartCoroutine (CreateMap());
}
void Update()
{
if (target != Vector3.zero)
{
if (path.Count > 0)
{
npc.transform.position = Vector3.MoveTowards(npc.transform.position, target, Time.deltaTime * speed);
if (npc.transform.position == target)
{
target = GetTarget();
}
}
}
}
// 创建地图
IEnumerator CreateMap () {
// 地图全局
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
yield return cube;
for (int i = 0; i < MazeItem.GetLength(0); i++) {
for (int j = 0; j < MazeItem.GetLength(1); j++) {
if(MazeItem[i, j] == 1) {
Instantiate(cube, new Vector3(i, j, 0), Quaternion.identity);
}
}
}
// 起始点标记
GameObject start = Instantiate(cube, new Vector3(xStart, yStart, 0), Quaternion.identity) as GameObject;
start.transform.localScale = Vector3.one * 0.3f;
start.renderer.material.color = Color.grey;
GameObject end = Instantiate(cube, new Vector3(xEnd, yEnd, 0), Quaternion.identity) as GameObject;
end.transform.localScale = Vector3.one * 0.3f;
end.renderer.material.color = Color.blue;
yield return new WaitForEndOfFrame();
StartCoroutine(CreateNPC());
}
// 创建NPC
IEnumerator CreateNPC()
{
GameObject npc_Prefab = GameObject.CreatePrimitive(PrimitiveType.Sphere);
yield return npc_Prefab;
if (MazeItem[1, 1] == 0)
{
npc = Instantiate(npc_Prefab, new Vector3(1, 1, 0), Quaternion.identity) as GameObject;
npc.renderer.material.color = Color.green;
target = npc.transform.position; // 设置初始点
}
yield return new WaitForEndOfFrame();
StartCoroutine(Pathing());
}
// 开始寻路
IEnumerator Pathing() {
if (GoPathing(xStart, yStart, xEnd, yEnd))
{
print("有路!!!");
}
else
{
print("没路!!!");
}
yield return new WaitForEndOfFrame();
}
bool GoPathing(int startX, int startY, int endX, int endY)
{
if (startX < 0 || startX >= MazeItem.GetLength(0) || startY < 0 || startY >= MazeItem.GetLength(1) || MazeItem[startX, startY] == 1)
return false;
MazeItem[startX, startY] = 1;// 防止重复走
if ((startX == endX && startY == endY) ||
GoPathing(startX - 1, startY, endX, endY) || GoPathing(startX + 1, startY, endX, endY) ||
GoPathing(startX, startY - 1, endX, endY) || GoPathing(startX, startY + 1, endX, endY))
{
// 存储路径点
path.Add(new Vector3(startX, startY, 0));
print("X:" + startX + "Y:" + startY);
return true;
}
else
{
return false;
}
}
// 获取路径
Vector3 GetTarget()
{
Vector3 point = npc.transform.position;
if (path.Count > 0 && n < path.Count)
{
point = path[path.Count - n - 1];
n++;
}
return point;
}
}
为了不让整个博客界面太长,请看下一篇《Unity3D之迷宫寻路_A*最短路径寻路》