http://blog.csdn.net/janeky/article/details/17457533
Most MMO games have an auto-pathfinding feature. When you click a location on the scene, the character automatically finds its way past. There may be a lot of obstacles in the middle, and the character will automatically bypass the obstacles and eventually reach the end. Using unity to develop a hand tour, automatic pathfinding can be implemented in many ways. The first more traditional is the use of a-star pathfinding, which is a more traditional artificial intelligence algorithm, in the game development is more commonly used. This technique is used in most of the page tours and end trips. In the Unity game can also use this technology, Asset store above already have related components, interested students can get to know. I'll have a chance to come back in detail later. Today we are going to learn Unity's official built-in pathfinding plugin-navmesh. Due to the more content, we will study in several times. Start by learning one of the simplest examples today.
(Reproduced please specify the original address http://blog.csdn.net/janeky/article/details/17457533)
We want to implement a function: Click a location in the scene, the character can automatically find the way past. The character bypasses all kinds of complex obstacles and finds a theoretical "shortest path".
1. Create a terrain
2. Add a role
3. Create multiple obstacles and try to make it as complex as possible to check the availability and efficiency of the Navmesh.
4. Select the terrain, and in the navigation window, set the navigation Static
5. Select the obstacle, and in the Avigation window, set the navigation Static
7.Navigation window, choose the bake (baking) interface, click the Bake button, process scene baking, you can bake out the road to the grid.
8. Add the Navmeshagent component to the role. Component->navigation->nav Mesh Agent
9. Add a script PlayerController.cs to the character, achieve click Target, auto pathfinding function
[CSharp]View Plaincopy
- Using Unityengine;
- Using System.Collections;
- Author:[email protected]
- Public class Playercontroller:monobehaviour
- {
- private navmeshagent agent;
- void Start ()
- {
- //Get 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.name.equals ("Terrain"))
- {
- 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 to determine if you have reached your destination, play idle or running animations
- if (agent.remainingdistance = = 0)
- {
- Animation. Play ("idle");
- }
- Else
- {
- Animation. Play ("Run");
- }
- }
- }
Finished, you can look at the effect:
1. The nav Mesh Agent component on the persona
Radius radius: The radius of the agent (for pathfinding purposes only, which can be different from the radius of the actual object, generally larger than the radius of the actual object).
Speed: The agent can travel around the world and move to its destination at maximum speed.
Acceleration acceleration: maximum acceleration.
Angular speed angular velocity: maximum speed (degrees/sec).
Stopping distance braking distance: braking distance. The distance to the destination is less than this value and the agent slows down.
Auto Traverse Offmesh link automatically traverse Offmesh Links: Automatically move and close offmeshlinks
Auto Repath automatically re-pathfinding: If the existing part is invalidated, obtain a new path.
Height altitude: The height of the agent (for debugging graphics).
Base offset basic shift: The collision geometry is offset vertically relative to the actual geometry.
Obstacle avoidance type obstacle avoidance types: The quality level of evasion.
NavMesh walkable navigation grid walk: Specifies the type of navigation grid layer that the agent can traverse. This parameter is useful and can be used in the following example.
2. The barrier must have mesh Render, used to bake the road-finding grid.
This example can be very simple for us to learn how to use the auto-pathfinding component Nav in the most basic way. However, this component also provides more powerful functions, such as blocking between the starting point and the target point,
What to do? In the following article, we will continue to explore other powerful features.
Http://pan.baidu.com/s/1hqC6v4o
1.http://www.xuanyusong.com/
2.http://liweizhaolili.blog.163.com/
3.http://game.ceeger.com/components/class-navmeshagent.html
Unity Hands-on tour < eight > Getting Started with auto-pathfinding Navmesh