Unity Navmesh nav Grid

Source: Internet
Author: User

  Purpose : The function to be realized is that you click somewhere on the map and the character moves toward that point. A bit of automatic search for the taste of the road.

Example : The Three Kingdoms of the Zhao Yun, the Three Kingdoms of the game inside the character movement can use this realization. There is a game that I don't like to play. League of Legends can also be implemented.

   What to do: create some new things first.

  

This player I have built an ellipsoid to use. When you're done, start following me.

First, open the navigation

  

Second, add navigation Static for the map

Check plane, tick navigation Static, you can now see the default is navigation layer, modified to walkable. If there is no walkable to layers inside yourself add it manually. Then bake in the lower right corner.

third, add the Navmeshagent component to the player, create a new Movetoclickpoint script for it, add it, and add the Rigidbody component to it.

The script reads as follows:

1 usingUnityengine;2 3  Public classMovetoclickpoint:monobehaviour {4 5 navmeshagent Agent;6     //Use this for initialization7     voidStart ()8     {9Agent=getcomponent<navmeshagent>();Ten      One     } A      -     //Update is called once per frame -     voidUpdate () the     { -         if(Input.getmousebuttondown (0)) -         { - Raycasthit hit; +             if(Physics.raycast (Camera.main.ScreenPointToRay (input.mouseposition), outHit -)) -             { +Agent.destination =Hit.point; Agent destination is the point of the click A             } at         } -      -     } -}

When everything is done, you will find that you click somewhere and the player will move to a certain place. But! Have you noticed the red cube of mine? The Red cube my intention was to make him an obstacle. But when you click on it, you find that the player doesn't. Okay, now come with me for the final step:

iv. Add navigation Static to the obstacle andNavigation layer changed to not walkable. Finally bake.

Now let's run the trial effect.

Unity Navmesh nav Grid

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.