Demand
Unity3d's 3d development environment, native with navigation components, can be easily and quickly realize the pathfinding function. However, there is no same function in native 2d.
Now many mobile phone games in the country have the function of automatic pathfinding, or the existence of some games such as robots, NPC, etc., all need to automatically find the road function.
The function I need to implement is similar to the movement of the gold digger in the FC game of the year. In the game there are gold miners, enemies, movable, non-moving areas, only along the straight line of the forward backward or upward downward.
Ideas
There are also 2d pathfinding plugins in the Unity3d. For example a pathfinding Project Pro and Navmesh 2D.
Two plug-ins are charge plug-ins, can meet different needs, but I used to find that is not the effect I want, and modify the code of others is very uncomfortable. So decide to write it yourself once A * algorithm.
A * algorithm has been a lot of Daniel's blog, have a very good introduction. I am looking at this article, recommend to everyone: transfer
The code in this article is that there is a problem with the processing of the inflection point, and I have modified it in the code below.
Solution Solutions
First the result, the path after the implementation is this. Please ignore that only into the dinosaur, he actually just came to soy sauce ~ ~
The core code is as follows:
1 PublicPoint-Findpath (Point-Start, point-end,BOOLIsignorecorner)2 {3 Openlist.add (start);4 while(Openlist.count! =0) {5 //find the point with the lowest F value6 varTempstart =openlist.minpoint ();7Openlist.removeat (0);8 Closelist.add (tempstart);9 //find the point that it's adjacent toTen varSurroundpoints =surrroundpoints (Tempstart, isignorecorner); One foreach(Point Pointinchsurroundpoints) { A if(Openlist.exists (point)) - //Calculate the G value, if it is larger than the original, do nothing, otherwise set its parent node as the current point, and update G and F - Foundpoint (Tempstart, point); the Else - //if they are not in the start list, join and set the parent node and calculate GHF - Notfoundpoint (Tempstart, end, point); - } + if(Openlist.get (end)! =NULL) - returnopenlist.get (end); + } A returnopenlist.get (end); at}
The Canreach direction of some minor problems in the original text, I modified the following:
1 Public BOOLCanreach (Point start,intXintYBOOLIsignorecorner)2 {3 if(! Canreach (x, y) | |closelist.exists (x, y))4 return false;5 Else {6 if(Math.Abs (X-start. X) + Math.Abs (Y-start. Y) = =1)7 return true;8 //If you are moving in a diagonal direction, determine if you are "mixing feet"9 Else {Ten if(isignorecorner) { One if(Canreach (Math.Abs (X-1), y) && Canreach (x, Math.Abs (Y-1))) A return true; - Else - return false; the}Else - return false; - } - } +}
Summarize
algorithm or to write some of their own, always take doctrine unfavorable to their own growth, in the university, the basic back to the teacher, the project of things, to use also to clear the use, to prevent the development of the future to leave hidden trouble, the time to start from the beginning to find the need to pay a lot of times.
Project source code contains the example of spine, so a bit large, interested friends can download to play.
Click to download source code
Some solutions to the path finding algorithm in Unity3d 2D game