In the game often will need to find the road, Asset store has a lot of related plugins. Here is a description of the Navmesh u3d comes with.
1. Topography
First create a new plane local table.
Then put some geometric objects on the plane as obstacles (note the path that allows the character to pass) and add rigidbody to the obstacles.
Group these obstacles with the surface (drag the obstacles to the plane under the sub-object) to facilitate the next operation.
Select Plane Group, open navigation window, tick navigation Static, prompt to set sub-object, click OK.
Click on the bake under the navigation panel and you will be prompted to save the scene, OK and save.
After baking (the blue part is the path that can be passed):
If the path of baking is unreasonable, the obstacle can be adjusted and then baked.
The tag of plane and its sub-objects is set to terrain.
2. Roles
Add a capsule when the role. Also add the Rigidbody component to it.
New Script Charactercontroller:
1 Public classPlayercontroller:monobehaviour2 {3 4 Privatenavmeshagent agent;5 6 //Use this for initialization7 voidStart ()8 {9Agent = getcomponent<navmeshagent>();Ten } One A //Update is called once per frame - voidUpdate () { - the //mouse Click Scene Set Target Point - if(Input.getmousebuttondown (0)) - { -Ray Ray =Camera.main.ScreenPointToRay (input.mouseposition); + Raycasthit hit; - //If you click the target point for the terrain + if(Physics.raycast (Ray, outHit )) A { at if(!hit.collider.tag.equals ("Terrain")) - { - return; - } - -Vector3 point =Hit.point; in //Character Steering -Transform. LookAt (NewVector3 (point.x,transform.position.y,point.z)); to //set the target point + Agent. Setdestination (point); - } the } * $ //is walking, the output hintsPanax Notoginseng if(Agent.remainingdistance >0) - { theDebug.Log ("seeking"); + } A } the}View Code
Run the scene and click on the target point to see if the path is successful.
A brief introduction to the Navmesh of Unity3d in the middle of the road