Unity Hands-on tour < eight > Getting Started with auto-pathfinding Navmesh

Source: Internet
Author: User

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)

    • Instance

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".

    • Steps

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
  1. Using Unityengine;
  2. Using System.Collections;
  3. Author:[email protected]
  4. Public class Playercontroller:monobehaviour
  5. {
  6. private navmeshagent agent;
  7. void Start ()
  8. {
  9. //Get 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.name.equals ("Terrain"))
  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 to determine if you have reached your destination, play idle or running animations
  36. if (agent.remainingdistance = = 0)
  37. {
  38. Animation.  Play ("idle");
  39. }
  40. Else
  41. {
  42. Animation.  Play ("Run");
  43. }
  44. }
  45. }

Finished, you can look at the effect:


    • Related knowledge

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.

    • Summarize

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.

    • Source

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

    • Resources

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

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.