A brief introduction to the Navmesh of Unity3d in the middle of the road

Source: Internet
Author: User

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

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.