Navigating Navmeshes - Tut 3
Navmeshes and Creating characters to use the Navmeshes in Unity
Navmeshes are prebaked paths that let AI know where is legal ground to walk on. Navmeshes can restrict where AI can go or let AI jump off ledges and is incredibly important to be able to control AI.
Creating Navmeshes
After opening the Navigation menu (Window->AI->Navigation) you are given a tab with 4 option. I selected all my objects that had Mesh Renderers and my Terrain and made them Navigation static and Generate OffmeshLinks, setting them as walkable.
Next you have to go to the Bake menu and press bake and this will create the Navmesh, it will take a few seconds. In the Bake windows you might have to tweak a few settings here as this will change how big of a slope will be added to the mesh and how small gaps can be to create a mesh in the gap.
This will make all your surface blue and will add circles to where you can jump down from. This is a good view of everything your AI can get to, its also worth checking if your Navmesh covers everywhere nicely. I personally had lots of issue with a set of stairs that I couldnt get working but it ended up being a mix of collider issues and not having the objects connected properly
Navmesh Agents
Navmesh agents are the Brains that let the AI be given a Coordinate and figure out how to get to it with pathfinding through the Navmesh and its all been beautifully done for you by Unity. Adding a NavMesh Agent to your AI character lets you assign its Destination Via scripts very easily, you can assign its Destination to any Vector3 but its better to go to a mouse click, which can be calculated with a raycast from your pointer to a world coordinate or to follow a character or object. To get the Navmesh Agent to follow the player all you need to do is Get the players position ( Through its transform.position variable) and its Velocity (Via the Players Navmesh) and then set the Objects NavmeshAgent Destination to the Position+ the Velocity. This will find the shortest path to the player while "Trying" to guess where you will be in a frame or two.
And here it is in action
Instanciating AI on mouse click
To get these AI in we need to spawn them in somehow, one way is to use the mentioned above technique of raycasting to figure out where the mouse is in the world coordinates and Instanciate a Prefab of the enemy of when we click. This isint as scary as it sounds either, all you need to do is wait for a mouse click, use the built in, ScreenPointToRay function on the camera using the Mouse position as the input, check if it hit and if it did instanciate an object there.
Not too complicated at all.
And that makes our prefab AI spawn at our mouse cursor whenever we click, here it is in action.
Conclusion
Navmeshes and Navmesh Agents are the fundamental parts of creating great AI in 3D games, Some tweaking and even some smarter AI choices will need to be made to create a good AI but this base is a great introduction into Navmeshes and following AI!
Get KIT207 Portfolio
KIT207 Portfolio
This is my Portfolio for KIT207
Status | In development |
Category | Other |
Author | Tylmcd3 |
More posts
- Probuilder and Terrain modelling in UnityAug 06, 2022
- Importing and using Blender Models in Unity - Tut DevlogAug 02, 2022
- The power and complexity of Blender AnimationJul 27, 2022
- Importing Models Into UnityJul 25, 2022
- Basics of BlenderJul 19, 2022
Leave a comment
Log in with itch.io to leave a comment.