U3D comes with navmesh automatic pathfinding teaching,

Source: Internet
Author: User

U3D comes with navmesh automatic pathfinding teaching,

Netease blog reposted by bloggers: Ah Zhao

Getting started with unity pathfinding Navmesh (1)

Note: from today on, I am planning to write some simple tutorials to help me review them later, or to help beginners learn. Do not blame the limited level. However, please respect the work of code screen recording. If you need to reprint it, please let me know first. Thank you!

 

Unity has added the NavMesh pathfinding feature since version 3.5. Previously, unity users can only use third-party plug-ins (such as the Astar routing plug-in) to find the path. I have also used A * pathfinding plug-in. The principle of A * is not complicated. If you are interested, you can Baidu it yourself. However, because it is not a built-in function, it is difficult to set the mesh and baking process. As a built-in function of unity, NavMesh has many similarities with the previous LightMapping baking or Occlusion Removal Occlusion Culling.

This feature is interesting and easy to use. I plan to separate several logs to introduce them one by one. The purpose of this article is to briefly understand the basic usage of NavMesh, so we will not translate the meaning of each parameter and function of the component in detail for the moment.

Let's take a look at how to open the NavMesh function window in unity:

In the window drop-down list, you can see Navgation. CLICK:

The Navigation panel will appear next to the Inspector panel: The Panel of the Objcet corresponds to the selected object, and the Bake panel next to it corresponds to the Global Options. The structure is the same as baking or Occlusion Removal. When an object is selected, the above content appears: The above All, MeshRenderers, and Terranis are used to filter the items displayed in the Hirarchy panel: all means displaying all MeshRenderers is to show only the rendered mesh objects, and Terrains is to show only the terrain objects. The following are important options. The first Navigation Static option is part of the selection of whether the object is used as a pathfinding function. Only when this option is selected can other options be operated. The OffMeshLink Generation option is to select whether the object will automatically generate the OffMeshLink based on global options such as height and hop bandwidth. This will be explained in detail in the future and will not be discussed for the moment. The Navigation Layer is a classification of map objects involved in the pathfinding function. It is classified by layers. By default, there are three layers to choose from. Of course, you can also add layers by yourself. Old Version: select Project -- NavMeshLayers from the Edit drop-down list. Note:: In the 4. after several new versions, the open position of the pathfinding Layer Management has changed, and it becomes in the Navigation panel: The NavMesh layer management interface is displayed: the above three Buit-in layers are the default three selectable layers of the system. We can enter the name of the desired Layer in the following User Layer, for example, if I enter a layer called "brigde", the "bridge" layer I just entered will appear in the selectable list through the steps just now, we have already done some of the commonly used NavMesh panels. Next we can make a small example: in the scenario, I put a Camera (Camera ), A plane that acts as the ground, a role model (man), and a target object (target) in order to facilitate observation of the position of the target point, I added the Light component to the target object to make it glow. You must add the NavMesh component to the role model (man). Otherwise, you cannot find the path. For convenience, I used the official big soldier model, which contains a script for animation and animation control. However, the performance of these animations is not important for the moment. You can choose to put a capsule or Cube to replace the character model. Do you still remember the Navigation panel you just introduced? Select plane and select the Static Navigation option in the Navigation panel. Click the Bake panel in the lower right corner. At this time, there will be a computing process. Friends who have used baking or occlusion removal should be familiar with this process. However, like the previous two functions, if you do not save the scenario level, unity will prompt you to save it first and then bake. Since there are very few objects in the current scenario, the Bake process will soon end. Check out the scene window. Now the color of the ground has changed. This is because unity has helped you generate a NavMesh for pathfinding. As there is no obstruction, therefore, the whole ground is within the walking range. Write the simplest control script. The above is C #. Because it is very simple, it is estimated that Js friends can also write the corresponding one. In a simple explanation, this script is directly dragged to the role (man), and the target object (target) in the scenario is dragged to the target variable of the script and assigned a value. At the beginning of the script, I first obtained the NavMeshAgent script component from man, and then, during the Update process, man constantly searches for the target's displacement and moves it to the target's position. After completing the above operations, you should have been able to click the unity play button and move the target object (target). At this time, the role model can be used to chase the target object. Here, I also made a simple function to set the target point by clicking plane. Because this function is not in the scope of this discussion, interested friends can go to Baidu for specific methods. At this step, the character has been running, but because there are no blocked obstacles, the character will only walk straight lines and cannot find the path. Next we will do something more complicated: Add a Cube in the scenario as an obstacle. Adjust the shape and position according to your preferences. I made it into a rectangle, and placed in front of the role. Similar to the operations on the ground just now, select the shelter Cube and check the Navigation Static option in the Navigation Panel. In this scenario, select Not writable. As the name suggests, this means that the Cube cannot pass. After the selection is complete, we click Bake again, which is a small waiting process. After Bake is complete, we return to the scene window. Observe the scene window and you will find that the whole ground is NavMesh and the situation has changed. Around the obstacle, NavMesh sets a gap, which indicates that the role can no longer pass through the obstacle. Click the play button of unity again. Now we can see that the character can walk around the obstacle and our goal has been achieved. This is the end of this example. In section 2, I will explain in detail the high/low gap, ladder climbing, and other complex functions. In section 3, we will explain how to find different conditions (different people take different paths) and how to dynamically control roads (such as hanging bridges.

Related Article

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.