Categories
Virtual World Building

Llama Drama Dev Blog – NavMesh

The best way to have a state machine path around an environment in Unity, I found, is by using a navmesh. I had briefly looked into them in a previous project, but due to the tutorials I was following leaving out a lot of the required code and details as to how to get it all to work, I didn’t go ahead with using it. So this was my first time using a navmesh.

I spent quite a bit of time just watching videos about how they work and the different components just to get used to them. I found Brackeys’ “Unity NavMesh Tutorial” series and LlamAcademy’s “AI Tutorial Series in Unity” on YouTube particularly helpful. I also found it pretty funny that I found a channel called LlamAcademy that has an entire playlist about navmeshes when I’m needing to learn about navmeshes to make a llama move around.

I started testing how navmeshes work in my test project, starting with a simple point and click navigation.

Point + Click Navigation
Point + Click Script

After that it was pretty easy to set up a navmesh. If an object isn’t going to be moving then set it to “Navigation Static” in the “Navigation” tab, check if you want to generate offmeshlinks for it, choose from the drop down if an agent can path on it, and bake.

Llama Drama NavMesh

I did see in several videos that you can use downloadable navmesh components instead of the built in navigation tab and it looks like they have much greater flexibility. I would like to try use them in the future, particularly for offmeshlinks. The built in navmeshlink component takes a start and end gameobject for it, whereas the downloadable components can designate entire areas to link between instead of the position of specific gameobjects.

The navmeshagent is pretty straightforward to set up too. It is annoying, though, that it allows you to make custom agents besides the default humanoid, but doesn’t like when you do. We had a lot of issues with custom agents where they would work fine for a while and then out of the blue Unity didn’t like them. We ended up having to go back to the default humanoid and change the values of that around, which isn’t such a big deal with just one agent in the scene, but if we had multiple different types of agent then it would’ve been very troublesome.

In the state machine, the llama teleports from one location to another when spawning into the office and running away to hide. Turns out, navmeshagents don’t like it when you use transform.position to set their new position. Sometimes it works, like it did for me for months, but at some point it will probably break and never work again. So use navmeshagent.warp() instead. It does the exact same thing only the agent will like you more for it. I haven’t had a problem with the teleport since. Just make sure you’re warping onto a navmesh.

Unfortunately, a problem has come about this last weekend. So navmeshagents have multiple coloured arrows that point in directions corresponding to the values of those arrows. One such arrow is blue, and the blue arrow points in the direction of velocity. The direction the agent is applying force to move is the direction the blue arrow points. Our problem is that sometimes when the llama spawns into the office workspace, that blue arrow is pointing straight up. Consequently, the llama doesn’t move when that happens because there’s no velocity being applied in any other direction, and the agent won’t leave the navmesh. We didn’t have this problem before and we have no idea how to fix it. The internet is not very helpful for this problem. Sometimes the arrow will point in the direction it is meant to, but it will flicker, and the agent with slowly move in that direction. It will constantly change speeds but never reaches the set speed it should be using. We’ve tried asking lectureres for help and they’ve given suggestions, but they haven’t worked either. So we’re stumped. I have no idea how to fix this problem and I’ve been trying all weekend to no avail.

Leave a Reply

Your email address will not be published. Required fields are marked *