Development environment: WIN10, Unity5.3.4, C #, VS2015
Date Created: 2016-05-09 I. INTRODUCTION
In most game scenarios, the areas that can be traveled are often not exactly the same. such as damaged roads, bridges, etc. will not be allowed to pass. So, how do you control moving objects? There are two ways to solve this problem.
The first solution is hierarchical control, and then dynamically change the available layers, such as for different bridges, with different layers to control. But this kind of processing has a limitation, such as a game scene contains a lot of bridges, each bridge has its own pass or prohibit state, then if the hierarchical control, then you need to separate each bridge layer, so-the number of layers is certainly not enough, because in unity can only be divided into 32 layers. Secondly, it is not easy to make frequent changes to the moving layers of objects when there are many moving objects.
The second solution is to use the Navmesh Obstade component to deal with similar dynamic roadblocks. As long as the component is mounted on a dynamic roadblock, the moving object will automatically evade these roadblocks when seeking the road. Ii. examples
This example focuses on the concrete implementation of the second approach. That is, the use of Navmesh Obstade components to deal with similar dynamic roadblocks. With this approach, you do not need to manually change the travel layer of the moving object, simply mount the Navmesh Obstade component on the bridge body, and then change the enable value of the Navmesh Obstade component through a script. When the bridge object is passable, the enable value is false and the Enable value is true when the deck is impassable.
1. Create a scene
In the Ch1201_navmesh_sample project, create a new scene named Demo4 that adds the following objects to the scene:
The cube under Demo4mask is static, the player is the moving object, and the Greencube is the destination.
2. Baking
The exact steps are similar to the previous sections, and the following are the results of baking:
3. Add nav Mesh obstable components to bridge
Select Bridge in the hierarchy view and add the Nav Mesh obstable component to it:
Once added, you can see that bridge is surrounded by a green grid.
4. Writing scripts
Add a script named Demo4BridgeControl.cs and change it to the following:
usingUnityengine;usingSystem.Collections; Public classdemo4bridgecontrol:monobehaviour{voidStart () {Startcoroutine (Init ()); } IEnumerator Init () {getcomponent<Renderer> (). Enabled =false; yield return NewWaitforseconds (2.0f); Getcomponent<NavMeshObstacle> (). Enabled =false; Getcomponent<Renderer> (). Enabled =true; } voidUpdate () {}}
The code uses yield return instead of timing, which is more convenient than just using a time variable to control timing, and the code is more concise.
5. Add script to Bridge
Drag and drop the script onto the bridge object.
6. Preview effect
Press the "Play" button to preview the game, and you will find that the capsule experience has been waiting for the bridge to reach its destination.
"Unity" 12.5 Navmesh Obstacle components