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