It’s amazing how in just a few lines, we can have a core of a stealth game written out! Granted, there’s some complicated mathematically specific sorcery going on that can seem intimidating, but I’m sure with a little time and practice, this will become second nature.
To begin with, we’ll create a simple cylinder to take place of our soon to be thief model, In case if you’re wondering what that blue color is, that’s our NavMesh baked into the floor, we’ll be going into that soon.
I placed it in the scene where Darren drops down as the capsule will eventually be replaced with the Darren gameplay model.
We’ll also need to rename the capsule to Player, tag it as player, and add a NavMesh Agent.
Next we’ll create a player script and attach it.
In order to find a point on the screen, we’ll need Raycasting. From Unity’s own page, let’s see what Raycasting is:
So first we’ll check for a mouseclick and use the raycast to get the information of where the mouse clicked:
We’ve actually gone through the hardest part of this challenge. The second part is using the NavMesh agent, and all you need to do is this:
but first we need to create a reference of our navmesh agent in our script and to do that, we’ll need to include the AI library:
Then it’s just a matter of creating a handle and using it!
The last couple lines just checks to see if there is an actual output to raycast and if so, place it into hitInfo, then have the navMesh guide the player to that point! Let’s see it in action!
Neat!!! I can’t emphasize enough how cool and easy things can be using Navmesh in Unity! This is awesome! Tomorrow we’ll go into a little more detail with NavMesh!