Learn a period of time to find the road, also learned a lot of algorithms on the Internet, today collated a bit, using the unity of the 3D interface used to find the road, the first is a simple pathfinding algorithm, the following is the map:
Map array:
Using unityengine;using system.collections;public static class Mapsarray {public static int[,] Mazeitem = new int[15, [] //init maze {{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}};}
Then there is the code for the simple path search:
Using unityengine;using system.collections;using System.collections.generic;public class Testpathing:monobehaviour { Private int[,] mazeitem; Initialize the maze private gameobject NPC; NPC Private list<vector3> path; Path private Vector3 target = Vector3.zero; Private float speed = 4; NPC move Speed private int n = 0; The currently moved road point 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; Initializes the maze array 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, Tim E.deltatime * speed); if (npc.transform.position = = target) {target = Gettarget (); }}}}//Create map IEnumerator Createmap () {//map global gameobject cube = Gameobje Ct. 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);}}} Starting point marker 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 ());} Create 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 G Ameobject; Npc.renderer.material.color = Color.green; target = npc.transform.position; Set the initial point} yield return new Waitforendofframe (); Startcoroutine (pathing ()); }//Start pathfinding IEnumerator pathing () {if (Gopathing (Xstart, Ystart, XEnd, yend)) {print ("There's a way!! ! "); } else {print ("No way!!! "); } yield return new Waitforendofframe (); } bool Gopathing (int startX, int starty, int endx, int endY) {if (StartX < 0 | | StartX >= mazeitem.get Length (0) | | Starty < 0 | | Starty >= mazeitem.getlength (1) | | Mazeitem[startx, starty] = = 1) return false; Mazeitem[startx, starty] = 1;//Prevent duplicate walk 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)) {//Storage path point path. ADD (New Vector3 (StartX, starty, 0)); Print ("X:" + StartX + "Y:" + starty); return true; } else {return false; }}//Get path Vector3 gettarget () {Vector3 point = npc.transform.position; if (path. Count > 0 && N < path. Count) {point = Path[path. Count-n-1]; n++; } return point; }}
In order not to let the whole blog interface too long, please see the next "Unity3d Maze seek road _a* Shortest Path pathfinding"
Unity3d Maze seeking Road using _ Direct pathfinding