"Go" unity comes with pathfinding Navmesh Getting Started Tutorial (ii)

Source: Internet
Author: User

http://liweizhaolili.blog.163.com/blog/static/16230744201271210237616/

The previous section briefly introduced the basic use of Navmesh pathfinding, this time to introduce a slightly more complex a little bit of high and low and jumping, first to see this goal:

Because the blog album Upload gif has restrictions, so I cut the whole process of 2 parts upload, the first part is to climb the stairs, the second part is the lateral jump and fall down

Whether it is climbing stairs, or jumping, navmesh all through the offmeshlink to do. There are two ways to create offmeshlink, which are explained by making the above example:

In order to do this, we pre-prepared some objects in the scene: The camera is necessary, a plane as the ground, and then f1--f5 a few different heights, L1 and L2 is the stair model, control the character body Man, as well as the target point of the move. The man must have a Navmesh agent component on it, which brings light components to the target for easy observation.

As mentioned in the previous section, the plane and f1--f5 steps check the navigation static option on the navigation panel, then bake, and observe the scene window, and you will find that we have generated the Navmesh grid we want, Now we can find and move the characters on the plane as we did in the previous section, but the characters won't climb the stairs.

At this time, we find the L1 staircase, in the beginning and end of the stairs to place two points, the two points only need to pick up its displacement, you can use empty gameobject to do, I here for the sake of observation, I took a cube to do. The start point is named StartPoint, and the end point is named endpoint.

Note: The position of StartPoint and endpoint is slightly higher than the plane.

The first method of generating Offmeshlink is described next. Select the L1 staircase and select Navigation--off Mesh Link in the Component drop-down option.

After the selection, the Offmeshlink component has been added to L1 's body, which we can see in the Inspector panel:

We assign the StartPoint and endpoint that were just placed in the scene to the start and end positions of the Offmeshlink component, and the other options do not change by default

Again bake

Now we find that in the scene panel, a line is generated between StartPoint and endpoint, and the direction is from StartPoint to endpoint.

At this point, you should be able to move the target to get the character to start climbing the stairs. But after climbing up the character can not jump down temporarily, if the target point moved to the plane, the character will climb down the stairs.

We use the same method to generate Offmeshlink for L2. At this point, the character should be able to climb two flights of stairs. The first goal has been completed.

Next we will make a second goal, first of all to analyze our scene:

We hope that people can jump from the height of 2.5M, more than 2.5M characters can not jump, too high will be dangerous. Then sideways we want the characters to jump over a 2-metre ditch.

According to this setting, our scene will be this situation: L1 and L2 can only by climbing stairs, L2 and L3 jump between, L3--L5 can jump down.

So, we found in the Navigation panel bake bar, drop height (drop altitude) fill 2.5,jump Distance (jumping distance) Fill 2, units are meters

Here's a second way to generate Offmeshlink:

We select the L1--l5 object and tick the offmeshlink generation option in the object bar of the navigation panel.

Bake again, back to scene window:

At this time, there will be a lot of heart offmeshlink in the scene, this is unity through the calculation, can jump or fall place automatically generated offmeshlink.

At this point, you should already be able to move the target points and let the characters jump and fall.

To get here, our second goal has also been completed.

However, some friends may ask questions, in the course of doing, if there is no model of the soldier, but instead of a capsule instead of the character, it climbs the stairs and jumps when it seems to be completed in a flash, there is no soldier that climb stairs and jumping action process. Like this:

This is true because the default Navemesh agent component is checked on the auto Traverse Off Mesh Link (auto through offmeshlink) option. This means that the character will automatically move to the end point of the offmeshlink as soon as it is at the Offmeshlink's starting point.

If you need to do your own control over offmeshlink, you need to write another script. I have a brief introduction to the method here, interested friends can try their own.

First of all, it is best to use the status to control the concept of roles. For example, the character can be divided into standing, walking, running, up and down stairs, horizontal jumping and falling down several states, for Navmesh, the characters can be divided into standing, normal Navmesh seek road, and through the Offmeshlink move several states.

Remove the Auto Traverse off Mesh link option first.

Then, when the character moves through the Offmeshlink state (which can be judged by Navmeshagent.isonoffmeshlink), get to the currently passed Offmeshlink:

Offmeshlinkdata link = navmeshagent.currentoffmeshlinkdata;

This allows you to get the coordinates of the start and end points of the link (Link.startpos and link.endpos), at which point your character can move with the simplest vector3.lerp, and when the character's displacement reaches the coordinates of the end point, the character's Offmeshlink movement state can To return to the normal path of finding or standing. In this vector3.lerp process, you can arbitrarily control the character's crawling or jumping movements.

Here, the contents of the second section are over. By now, you should have been able to master the Navmesh pathfinding all the normal operation. Next, I will briefly explain the Navmesh in the third section of the slightly complicated control method, I hope you continue to support Zhao.

Incidentally, many friends may wish me to download the engineering documents. However, I think this is not a good thing, because these examples are very simple operation, if you have the patience to follow the Azhao step down, you should be able to get the results you want. And if you don't have the patience to look at the project file directly, you may overlook a lot of important steps.

"Go" unity comes with pathfinding Navmesh Getting Started Tutorial (ii)

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.