Development environment: WIN10, Unity5.3.4, C #, VS2015
Date Created: 2016-05-09 I. INTRODUCTION
This section shows a simple example of how to implement a navigation grid using a static object, and a dynamic object that uses the navigation grid to find the path automatically and finally finds the target. Second, the design steps
1. Add 3 cubes
Start the Unity application to open the Ch1201_navmesh_sample project, create a new scene named Demo1-1.unity, and build 3 cubes in the scene, as shown in:
2. Generate Navigation Grid
(1) Change all 3 cubes to static
Select the 3 cube objects in the game scene, click Static in the Inspector view to make them static (for unity to use these static game objects to generate the navigation grid), and then right-click the drop-down button on the right of static. You can see that it has automatically checked the following check boxes as shown in:
(2) Show Navigation view
Select window, Navigation, to display the navigation window:
The Navigation tab appears on the right side of the viewer view.
(3) Set baking (Bake) parameters
Switch to the Navigation view, and then switch to the Object tab to see that it defaults to "all" and then to the "Bake" tab, changing the parameter to the value shown (this is done so that the character can walk along the ramp to the top of the Red Cube), Click the Bake button below when you are done with the modification:
In this case, you'll see the baking effect in the scene:
This completes the navigation grid.
3. Add Navigation target
Add a green cube above the Red cube and change its name to Greencube, which will serve as the target for navigation pathfinding, as shown in:
4, Seek the road
(1) Moving object for adding pathfinding
Create a new capsule body component in the scene capsule, which will act as the object to be traveled:
Name the capsule as player, and in the viewer view, set scale to (0.2,0.2,0.2) as shown in:
Select the player, and then select Component, Navigation, nav Mesh Agent, to add a navigation component to the capsule:
You will see a light green mesh body around the capsule body:
(2) Writing C # scripts
Right-click Assets and add a C # script that will be used to automatically find the capsule body navigation target:
Name the script Demo1Test.cs, and then double-click the file, and unity will automatically open it in VS2015 and change the file to the following code:
using Unityengine; using System.Collections; public class runtest:monobehaviour{ public gameobject TargetObject = null ; void Start () { if (TargetObject! = null <NavMeshAgent> (). Destination = TargetObject.transform.position; }} void Update () {}}
(3) Adding a script to a capsule body object
Save the Demo1Test.cs script, and then switch to unity, and drag the script to the capsule body object (player) in the hierarchy view, as shown in:
At this point, the capsule body is set up as a moving object to complete the work.
(4) Set up pathfinding targets
Select the capsule body object, and then drag and drop the greencube onto the value of the Target object property in the viewer:
(5) Preview Navigation pathfinding effect
Click the Play button to see the animated effect of the capsule body auto-pathfinding. At this point, the capsule will automatically find the right route and reach the target (Greencube) along that route.
(6) Observing the automatic path-finding process of the target under different position
Move the Green cube to another location, and click the Play button again to see the animated effect of navigation pathfinding.
5, change the target position to observe whether drilling arch
(1) Moving the target to the other side of the bridge
Save the scene and then save the scene as Scene1-2.
Open the Scene1-2, move the capsule body to the left side of the bridge, and move the Green cube to the other side of the bridge, as shown in:
Click the Play button to observe the effect of the capsule body auto-pathfinding. At this point, the capsule can be found to bypass arch from the side to seek the road to the target. That is, it does not automatically drill arch.
(2) Allow the capsule to be drilled through the body arch
Save the scene and then save the scene as Scene1-3.
Open Scene1-3, in the Navigation view, change the value of Agent Height to 0.2, and then click the Bake button to bake, as shown in:
Press the play button, and you'll see the capsule drilled through the arch to the destination (Greencube) instead of bypassing the bridge obstacle to find the target.
Simple example of "Unity" 12.2 navigation grid pathfinding