A* Pathfinding and Random Waypoints [ADVANCED Patrolling]

OK so one of the bugs I noticed whilst testing my game, by accident, is that if you get (or place) something in the way of your guard (or any NPC in that case), they literally don’t know what to do… they’ll basically brutally force their way into whatever is ahead of them

I did some research about this, and discovered something known as the “A* Pathfinding Algorithm”, which basically seems like it deals with obstacles in the path of NPCs following an assigned patrol path

I get the concept, but so far all I managed to find was an implementation by Code Monkey for a grid-based system, and I don’t have a grid-based system in my project, so I wanted to ask:

How do we implement one in our RPG to Third-Person project? (that, and random waypoint indexes)

You don’t. You don’t need it. You just need to put a ‘NavMesh Obstacle’ component on whatever you put in front of the NPC and it will cut the navmesh so the AI built into the navmesh will pathfind around the obstacle

pick a random waypoint

nextIndex = Random.Range(0, waypoints.Length);

ahh… it’s not that simple, OK here’s a little (now it’s public) secret:

I recently started working with Malbers on trying to implement his horse riding system into my personal project, and dismounted my horse in front of my test patrolling NPC (this one was a scaling and animation nightmare to implement, and it’s about to get a bit harder…). The horse isn’t static, because it does get driven around the game map. Let’s just say the enemy tried brutally forcing his way into the horse itself… It was a weird one (hence why I seek help with this algorithm, or anything that can solve the problem of dynamic obstacles getting in the way)

Still no change. Obstacles can move and it will dynamically update the navmesh.

that works in Unity 2021 LTS as well? As far as I can recall, Brian mentioned that the NavMesh had a significant change in Unity 2022 (hence why I’m hesitant to upgrade right now)

Who still uses 2021???

Lemme see

oh it has it… I found one :slight_smile:

lol I tried upgrading once to 2022, I’d rather not try that right now :stuck_out_tongue_winking_eye: (at least not with this project)

You don’t.

If only the NavMeshTools had something like a NavMeshModifier that could be placed on something you dropped into the world…

It works in even older versions than 2021, it’s only forced on you unceremoniously in 2022.2 or later.
Go to the Package manager, and click on the drop down in the upper left hand corner and select install from Git URL.

Then enter


That installs the NavMeshTools, then you can follow the instructions in the Third Person Tutorial for switching over to the NavMeshSurface/NavMeshModifier.


lol I never dealt with Experimental packages before, but I trust you… I’ll go finish off that tutorial in the meanwhile as we wait for your next topic as well :slight_smile:

Experimental is a big word, in this case.
Unity started working on the AI Navigation package in Unity 5.0 (I’ve been using it since around 2016 or so). In fact, I haven’t used the legacy Navigation at all since 2018 ror so.

trust me, that’s what is written when trying to install the package :stuck_out_tongue_winking_eye:

anyway, I’ll be off to give it a go (question on the fly, impact states comes out tomorrow right?)

This is the tutorial, right? 05 Upgrading the NavMesh · Wiki · Tkraindesigns / RPGThirdPerson · GitLab

That’s the one.
And yep, before Unity 2022.2, it’s marked as experimental, but it works great.

1 Like

lol I was quietly horrified of the whole “big bosses won’t work with the current NavMesh” for a while… alls good though, I’ll follow along and work on tutorial #13 as well next (the one about the trees, although mine are instance trees (in other words, they spawn in the game scene as the game plays))

why can’t I see my NavMesh Changes change like that video…?

and for god knows what reason, my enemy cannot walk anymore. Here’s the changes I did so far:

  1. Unbaked the old NavMeshAgent
  2. Installed the package, and then went to GameObject → AI → NavMesh Modifier Volume
  3. On one of my floor tiles (I might delete the rest and make one large tile instead, that sounds like a better idea), I added the “NavMeshSurface”, and then baked that…
  4. Created a new “Character” Layer, and assigned it to my test enemy (I temporarily eliminated my old enemies so we can focus on getting just the first one to work, for now…), and then went to the NavMeshSurface on my floor, changed “Include Layers” to “mixed” (i.e: I eliminated the “Character” layers)

But he still refuses to walk… Did I miss something? I tried following the instructions but I think I may have missed something

I also have to mention that the targeting state had a bug when I placed the enemy on another spot for testing purposes (and when it worked, it focused on an entirely different area that had nothing to do with the enemy itself… it took a couple of tries before we got it working again. Check the first image under the NRE, the red area is my point of focus, and the blue one is where I expected the player to focus).

It gave me this NRE (it’s hard to get that one again, but somehow… it’s blaming @bixarrio 's crafting system for its lack of focus… now I’m genuinely confused), everytime the enemy hits me (and when I die, it no longer resets…):

NullReferenceException: Object reference not set to an instance of an object
RPG.Crafting.CraftingSystem.CloseCrafting () (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/CraftingSystem.cs:169)
UnityEngine.Events.InvokableCall.Invoke () (at <ba783288ca164d3099898a8819fcec1c>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <ba783288ca164d3099898a8819fcec1c>:0)
RPG.Crafting.ShowHideCraftingUI.OnModalActive () (at Assets/GameDev.tv Assets/Scripts/UI/ShowHideCraftingUI.cs:28)
UnityEngine.Events.InvokableCall.Invoke () (at <ba783288ca164d3099898a8819fcec1c>:0)
UnityEngine.Events.UnityEvent`1[T0].Invoke (T0 arg0) (at <ba783288ca164d3099898a8819fcec1c>:0)
RPG.Attributes.Health.TakeDamage (UnityEngine.GameObject instigator, System.Single damage, RPG.Skills.Skill skill) (at Assets/Project Backup/Scripts/Attributes/Health.cs:177)
RPG.Combat.Fighter.TryHit (System.Int32 slot) (at Assets/Project Backup/Scripts/Combat/Fighter.cs:543)

and when I punch, it teleports the player, as shown below (the next image is a few blocks on the z-axis behind where he was right before punching…):

Are you sure this is a good idea…? (I’m no expert with NavMesh agents, but boy did this put my project in a terrible state… :sweat_smile:)

and finally, here’s the NRE that comes from the failed targeting state:

NullReferenceException: Object reference not set to an instance of an object
RPG.States.Player.PlayerTargetingState.Tick (System.Single deltaTime) (at Assets/Project Backup/Scripts/State Machines/Player/PlayerTargetingState.cs:33)
RPG.States.StateMachine.Update () (at Assets/Project Backup/Scripts/State Machines/StateMachine.cs:18)

which is basically leading to this line in ‘PlayerTargetingState.cs’:

            FaceTarget(stateMachine.Targeter.CurrentTarget.transform.position, deltaTime);

I reversed the project… I think we need to address a lot of issues before I try changing the NavMeshAgent

I don’t think you need the NavMesh Modifier Volume. It is used to make whole areas unwalkable. For our purposes, you just need the NavMeshSurface and the agents, plus the obstacle on the horse.

I’m just shocked of the ton of bugs that suddenly showed up out of this one… it still baffles me what the Crafting System has to do with the targeting state for example

Every time you add something to your project there are a lot of issues. :slightly_smiling_face: I am baffled too!

lol tell me about it… Anyway, if I’m not the one to fix it, no one will, so I better get to working on it!

I tried that again, but changed ‘NavMesh Modifier Volume’ in step 2, to ‘NavMeshSurface’ thanks to @edc237 's suggestion, and… well it works now, but in a bit of a weird state

but… whilst my enemy won’t seek a path to his original patrol state, he will follow me through terrain he’s not allowed access on to (Edit for that: I turned on the gizmos and saw why it’s happening… we didn’t clear the NavMesh completely as of yet). All is good for now though, I’m still testing it all…

Edit 2: LOL my whole NavMesh was probably over-baked… I have layers over layers over layers of NavMesh to clear off… is there any easier way to clear everything out and re-bake?

1 Like

Privacy & Terms