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
- Using Unityengine;
- Using System.Collections;
- Public class Playercontroller:monobehaviour
- {
- private navmeshagent agent;
- public bool Setagentwalkmask; Whether you need to dynamically modify the pathfinding layer, in the instance of Scene4
- void Start ()
- {
- //Get pathfinding components
- Agent = getcomponent<navmeshagent> ();
- }
- void Update ()
- {
- //left mouse button click
- if (input.getmousebuttondown (0))
- {
- //camera to click on the ray of the location
- Ray Ray = Camera.main.ScreenPointToRay (input.mouseposition);
- Raycasthit hit;
- if (Physics.raycast (Ray, out hit ))
- {
- //Determine if the clicked Terrain
- if (!hit.collider.tag.equals ("Plane"))
- {
- return;
- }
- //Click location coordinates
- Vector3 point = Hit.point;
- //Steering
- Transform. LookAt (new Vector3 (Point.x, TRANSFORM.POSITION.Y, point.z));
- //Set Target points for pathfinding
- Agent. Setdestination (point);
- }
- }
- //Play animation
- if (agent.remainingdistance = = 0)
- {
- Animation. Play ("Idle");
- }
- Else
- {
- Animation. Play ("Run");
- }
- }
- }
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
- Dynamically set Pathfinding path layers
- void Ongui ()
- {
- if (!setagentwalkmask)
- {
- return;
- }
- if (GUI. button (new Rect (0, 0, +), "Go Lower"))
- {
- Agent.walkablemask = 65;
- }
- if (GUI. button (new Rect (0, +, +), "walk Up"))
- {
- Agent.walkablemask = 129;
- }
- }
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
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!
Http://pan.baidu.com/s/1jGoLITo
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