AI Chase if player too far

Hello all!

I’ve realized that I am lacking some basic knowledge. I’ve made a script for AI to follow trigger points throughout the map, but how would one go about at making an AI chase player if he is too far from AI? As soon as AI is close enough to the player then the AI would have to continue on walking through the trigger points.

I am making an audio game intended for blind people and the AI is essentially like a guide. Looking after the player and making sure he doesn’t get lost in the map.

Thank you!

If you’re using a behavior tree then it’s pretty straight forward. You just need to create a new descriptor that tests (distance-to-player >= max-distance). Then add a selector node instead of your existing pathing logic. Add a MoveTo node under the selector and give it the descriptor you made. Then add to the selector your logic that follows the trigger-points (you’re kind of inserting the selector to provide a decision point).

So now if the descriptor passes (returns true), it will trigger the MoveTo (near player location) but if it returns false, it will trigger your pathing logic. If you want the MoveTo to interrupt the pathing at any time then you can select the descriptor in the MoveTo and in the details panel, change “Observer Aborts” to “Lower Priority”. Otherwise, he will go all the way to the next trigger-point before he realizes the player is out of range.

Hope that was clear enough. If you are unfamiliar with behavior trees (descriptors and such) then we can find you a good tutorial video. You’re also welcome to post an image of your existing behavior tree and we can find the right spot for the selector and its sub-nodes.

Behaviour trees… okay.

I am currently using a blueprints with script as shown in the picture. that is basically how the character moves from target point to target point. I was thinking that what if I store first person player location as a float and constantly make the AI check if the player is within a range? If player is not in the range then chase and SpawnSoundAtLocation (to tell the player to follow AI) until player is within range, then continue on with journey.

Is my method inefficient? Should I redo the whole walking flow in a behaviour tree?

This is the kind of logic that I was thinking would work, hence if the variable is true, the AI stops whatever is doing and starts a chase script.

something like this should help to get you started. basically every one second the distance to the player is checked and if it is greater than 300 units the ai moves to the players location.

Yeah, without a BT is even easier. Not sure why I didn’t include that just in case. But you also don’t want the AIMoveTo and this mechanism to fight each other. You should be to add your trigger-point following to the True pin of that branch. Added bonus is you won’t be doing it on tick anymore which saves CPU. Just adjust the Time value of the SetTimer node so the AI is responsive enough but no more than that.