Use of the astarpathfinding plug-in unity3d

Source: Internet
Author: User

Disclaimer: the original articles of this blog are all personal originals and are copyrighted. For more information, see http://blog.csdn.net/ml3947. for more information, visit http://www.wjfxgame.com.


Many people should be familiar with astarpathfinding. This is the best path finding plug-in unity3d. Whether it's 3D games or 2D games, we can use it to find the path of the * algorithm. In the previous tutorial "using free tools for 2D game development", there should have been an astarpathfinding path search section. However, after three sections of this tutorial are translated, I found that the author just gave the script and didn't introduce it. The introduction to orthello ended in the previous sections, so I didn't continue to translate it. Here is another article to introduce the path finding.

Let's take a look at how to use astarpathfinding.


First, create a game scenario.

As you can see, this is a terrain, and several houses, lights, trees, and so on are placed.

Create an empty object and add a script through component-> pathfinding-> Pathfinder. This is the most important script in astarpathfinder. Change it to astar.



We can see many columns on the right.

In fact, the principle of path finding is still distributed to the ground in the form of a grid, to eliminate the corresponding layers of some obstacles, or to perform a height test so that a certain height cannot walk. Then, use the astar path search algorithm to find the shortest path.

Create a grid graph.


Then adjust the number and size of the mesh here.

For our 3D games, the grid size can be set to 1 (2D games are usually set based on the map cell size ). Set width and depth to adjust the size of the entire grid. Remember that the height must be the same as the terrain height.

Scan at the bottom of the last point. All walking paths are searched and displayed on the grid in a certain color.


There are several important parameters.

The first is Max climb and climb axis. This refers to the maximum climbing height and the direction of the climbing axis. Generally, this axis must be perpendicular to the terrain, otherwise it will not be able to pass through all the places on the ground.


Then collision test. Here is some collision detection in the path finding.

For example, if we perform Collision Detection on the default layer, the path will not pass through the perimeter of the tent during path search.



Let's take a look at the inner, where a dish of food is placed in the middle, and the path cannot be found. It can be found in other places.



When we deselect collision test and scan it again, the entire tent range will be included in the walking range in the path searching.


Remember the moving range of the mesh on the path finding does not mean that your role can walk from these ranges. It only indicates that the pathfinding algorithm will include these scopes as feasible. Then, if your role is in conflict with the tent, even if the path calculated by astar is to be used as a tent, your role cannot do the same.


When the Pathfinder of astar finds the path, a green line is displayed in scene.

This green line is the path to be searched.



Similarly, by limiting layer collisions, we can also use astarpathfinding in 2D games. For example, although the Green Line is not very clear, we can still see that monsters go to our role path.


Next, let's take a look at how to process the path after finding the path. Here are some simple scripts in the above 2D game.


public void Start (){controller = GetComponent<Controller> ();seeker = GetComponent<Seeker> ();seeker.pathCallback += searchComplete;StartCoroutine(RepeatTrySearch());}IEnumerator RepeatTrySearch(){while(true){  TrySearchPath();  yield return new WaitForSeconds(searchRate);}}IEnumerator WaitForRepath(){if (waitingForRepath) yield break; waitingForRepath = true;     yield return new WaitForSeconds(searchRate - (Time.time-lastRepath)); waitingForRepath = false; TrySearchPath();}public void TrySearchPath(){if(Time.time - lastRepath >= searchRate && isCanSearch && isCanSearchAgain){lastRepath = Time.time;  seeker.StartPath (transform.position, target.position);isCanSearchAgain = false;}else {    StartCoroutine(WaitForRepath());}}public void searchComplete (Path p){path = p;nowPosition = 0;if (path != null) {vPath = path.vectorPath;nextNode = vPath [nowPosition];Debug.Log ("Find Path:" + vPath.Count + "," + vPath [0]);} else {Debug.Log ("Find No Path");}isCanSearchAgain = true;isMoveFinished = false;}

This is a code for repeated search paths. Because our roles are constantly moving, monsters need to change the target location for a certain period of time and perform secondary search.

After path finding, we can use path. vectorpath to get a path array, which is a list <vector3> linked list. Records the coordinates of each vertex in the path. We can move and process points one by one.

This is a simple process of astar path finding.


Reprinted please indicate the source: http://blog.csdn.net/ml3947


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.