Unity comes with pathfinding Navmesh Getting Started Tutorial (iii)

Source: Internet
Author: User

Continue to introduce the function of Navmesh pathfinding, next Azhao intends to talk about the following two examples, first look at the finished effect:

The first example for a friend who likes DotA should be very familiar with, is the different teams of different routes to attack the road, red and green blue three teams on the basis of three routes offensive.

The second example is a dynamic switch that controls the road. When the drawbridge is open, the road is impassable, and when the drawbridge is lowered, the road can be used.

First of all, the first example is actually very simple, just need to modify the layer settings on the basis of the previous two sections.

First to analyze our terrain, in order to do this example, I prepared two plane as our base and local base, and then use plane simple to spell three lines.

At our base, we prepared three soldiers of red, green and blue (with capsules instead). Of course, a soldier with a Navmesh agent component

If the two plane and three lines are navigation static words, and the ordinary seek road no difference, three roads can walk. Next we are going to set the layer.

Remember how you set Navmeshlayer in the first section?

Now we need to set up the red, blue, and green three layers as shown.

Now, we can select route one and then select the red layer inside the navigation layer. Accordingly, route two choose blue Layer, route three choose green Layer

Bake out, it will be the effect of the above.

The road is set, and then we set the Navmesh agent component on the creep.

Choose the blue creeps and find the Navmesh walkable option inside the Navmesh agent component, which is the choice of layers that the character can pass through. According to the design of the layer, blue creeps need to check the blue layer and deselect the red and green layers.

Similarly, the red creeps and green creeps Navmesh walkable layer.

Now, we can run, move the target to the enemy base, three different colors of the creeps will be based on different routes to find a way to attack.

Our first goal has been completed. Let's talk about the second example. I have prepared two platforms, the middle is an active suspension bridge, a role model with Navmesh agent components, a target point if you follow the previous operation, we can easily let the character in any position to seek the road, but in order to let the suspension bridge rise when the role cannot pass the drawbridge , we need to make some settings for the Navmesh layer of the drawbridge.  As we have just done, we are going to set up a layer called bridge in advance (as in the first section).       Similarly, the suspension bridge chooses the bridge layer here the operation and the above example is not different, but this character body's Navmesh walkable does not need to set, the direct everything is OK. Here, we need a script to control whether the character can pass through the drawbridge.      You need to use Navmeshagent.walkablemask to dynamically modify the value of the Navmesh walkable.      On how to lift the drawbridge, this is not the content of this discussion, you can choose to control directly with the displacement, or the suspension bridge directly to do the animation of the switch is OK. As I said before, you'd better have a habit of using state to control objects. The drawbridge here will have 2 states, one is open and one is off. When open, the drawbridge is disconnected, the characters cannot pass, the suspension bridge closes and the person can pass. Given both of these States, we assign values to the Navmeshagent.walkablemask of the character. Write a very simple code: private bool open = false;
Private navmeshagent agent;void Start () {
Agent = gameobject.getcomponent<navmeshagent> (); }void Update () {if (open) Agent.walkablemask = 1;else Agent.walkablemask =-1;} Yes, it's that simple. Here for the sake of simplicity, the assignment is written in the update, in fact, this is not correct, do not need to detect every frame assignment, just need to write this assignment in the suspension bridge switch control inside the line. Let me explain what it means: Define a state switch open, open= True when the drawbridge is open, open = False when the drawbridge is closed, and how to assign values to this state, you can try it yourself. Then assuming that the script is placed on the character, the Navmesh agent component of the character is acquired at start. Of course, you can also put the script in other places, and then assign a value to specify the agent. When Open is true, the character's Walkablemask = 1, meaning that only the first layer (which is default) allows the player to pass. When Open is false, the character's walkablemask =-1, which means that all layers (everything) can be passed by the player. From the interface of the custom layer we can summarize, each layer of the value, in fact, is comprehensive so that the sum of the value of the layer added 1. or 2 (n-1) of the second party.        In addition to specifying the layer itself unexpectedly, you can also specify multiple layers, for example, if you want to mix a default and bridge layer can pass, you can set Walkablemask = 9, that is, 1+8. So far, Zhao, my introduction to Unity's own Navmesh search is over. As for the Navmesh or Navmesh agent components of the various properties and methods, the API document contains specific instructions, interested can be specific to see, I do not explain. There are many changes in the usage, you can try it yourself. Thank you for your support!

Transferred from: http://liweizhaolili.blog.163.com/blog/static/16230744201271225812998/

Unity comes with pathfinding Navmesh Getting Started Tutorial (iii)

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.