Unity Hand Tour < nine > Auto pathfinding navmesh Advanced Topics

Source: Internet
Author: User

http://blog.csdn.net/janeky/article/details/17492531

Before we learned how to use the Navmesh component to achieve the most basic role of automatic pathfinding. Today we will continue to explore the advanced features of the navigation components in depth. In this article, you will learn how to automatically generate a pathfinding grid in two isolation layers. How do I manually specify a route for a road-finding grid? As well as the application of the grid of road-searching. (The models used in this article come from the Unity3d website).

(Reproduced please specify the original address http://blog.csdn.net/janeky/article/details/17492531)

    • The isolation layer automatically generates a road-finding grid

(Source scene1.unity)
1. Creating a Plane instance p1,p2, there is a gap between the two. Direct control of character displacements is not possible.
2. Open the Navigation window and select P1,P2 respectively to set navigation Static and Offmeshlink generatic

3. Save the scene and click the scene Bake button bake. After the end we can see that p1,p2 in addition to the production of their own road network, they directly also generated a link.
4. Add the role model solder, add navmeshagent (component->navigation->navmeshagent) to it
5. Add Playercontroller script to Solder

[CSharp]View Plaincopy
  1. Using Unityengine;
  2. Using System.Collections;
  3. Public class Playercontroller:monobehaviour
  4. {
  5. private navmeshagent agent;
  6. public bool Setagentwalkmask; Whether you need to dynamically modify the pathfinding layer, in the instance of Scene4
  7. void Start ()
  8. {
  9. //Get pathfinding components
  10. Agent = getcomponent<navmeshagent> ();
  11. }
  12. void Update ()
  13. {
  14. //left mouse button click
  15. if (input.getmousebuttondown (0))
  16. {
  17. //camera to click on the ray of the location
  18. Ray Ray = Camera.main.ScreenPointToRay (input.mouseposition);
  19. Raycasthit hit;
  20. if (Physics.raycast (Ray, out hit ))
  21. {
  22. //Determine if the clicked Terrain
  23. if (!hit.collider.tag.equals ("Plane"))
  24. {
  25. return;
  26. }
  27. //Click location coordinates
  28. Vector3 point = Hit.point;
  29. //Steering
  30. Transform.  LookAt (new Vector3 (Point.x, TRANSFORM.POSITION.Y, point.z));
  31. //Set Target points for pathfinding
  32. Agent. Setdestination (point);
  33. }
  34. }
  35. //Play animation
  36. if (agent.remainingdistance = = 0)
  37. {
  38. Animation.  Play ("Idle");
  39. }
  40. Else
  41. {
  42. Animation.  Play ("Run");
  43. }
  44. }
  45. }

5. Click on any of the positions to see the characters can automatically find the way past



    • Manually specifying the orientation of the seek grid

SOURCE Scene2.unity
Based on 1.scene1.unity, the Offmeshlink generatic of the P1,P2 is removed
2. Create a new empty Gameobject start,p2 on P1 on the new empty Gameobject End
3. Select start to add the off Mesh link component to it component->navigation->offmeshlink
4. Set the properties of the off Mesh link component and Start point to Start,end point to End
5. Bake the scene. We can see a link from start to end
Click on the map, you can see the character if you want to cross P1 and P2, it must be along the path we created manually


    • Navigation grid Obstacle Navmesh obstacle

We used to use fixed objects as obstacles before baking the scene. Unity also provides dynamic obstructions. Any gameobject can add navmesh obstacle components and become an obstacle. The concrete steps are component->navigation->navmesh obstacle. It has two properties: radius and height, which can be set to the same size as your item.

    • The application of the grid of the searching net

SOURCE Scene3.unity
Played "3c", "DotA" This kind of game students know: on the map on the next three avenue, different soldiers may go different roads. Today we also make a similar example.
1. New P1,P2,P3,P4, such as 4 plane, the specific configuration see
2. In the navigation window, add two layers layers:blue layer and red layer
3.P1,P2 navigation layer is set to DEFAULT,P4 navigation layers set to RED,P3 set to blue
4. Add two characters to set their navmeshagent pathfinding layer (NavMesh walkable). One to remove the red layer and one to remove the blue layer
5. Click on the coordinates of the P2, you can see them along the different paths to the target point, one to go to the level of the route, a lower course.



    • Dynamically changing the grid layer of the search network

SOURCE Scene4.unity
In the game, we often need to choose different paths according to different conditions.
1. Make a change on the basis of scene3.unity. Keep only one role
2. Add two buttons, "Walk up" and "Go Down", when the game is running, you can change the agent's pathfinding layer.

[CSharp]View Plaincopy
  1. Dynamically set Pathfinding path layers
  2. void Ongui ()
  3. {
  4. if (!setagentwalkmask)
  5. {
  6. return;
  7. }
  8. if (GUI. button (new Rect (0, 0, +), "Go Lower"))
  9. {
  10. Agent.walkablemask = 65;
  11. }
  12. if (GUI. button (new Rect (0, +, +), "walk Up"))
  13. {
  14. Agent.walkablemask = 129;
  15. }
  16. }

3. Re-click to find the road, you can see, choose a different pathfinding layer, the role of the path is also different

See the code in the Agent.walkablemask = 65 and 129, we will be more confused, in fact, each layer of the pathfinding layer is a power of 2, see

So the upper mask = default (1) +blue (128) = 129, Lower MAK = Default (1) +red (64) = 65

    • Summarize

This article will find the path of the components of the various details have done a comb, in detail to help you master each feature, in the actual project according to the different needs of skilled selection of application-related features. Next I will have a more detailed example to run through the Navmesh features, please look forward to!

    • Source Address

Http://pan.baidu.com/s/1jGoLITo

    • Resources

1.http://www.xuanyusong.com/
2.http://liweizhaolili.blog.163.com/
3.http://game.ceeger.com/components/class-navmeshagent.html

Unity Hand Tour < nine > Auto pathfinding navmesh Advanced Topics

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.