Behavior for long range enemy when player is too close

EDIT: sorry I posted in the wrong forum, I followed a google link and it took me to previews forum instead of the main forum, i reposted the thread here: Behavior for long range enemy when player is too close
feel free to delete this thread, mods

  • I’m working on the AI system for an action game. My question is about pathfinding. I have a long range pathfinding a position a bit far from the player say I want it to be 5 units away for it to stop pathfinding and do an attack. What should I do if the player gets too close? how can I tell the enemy where to go if it’s TOO CLOSE
  • keep in mind the levels are randomly generated so I can’t make predetermined spots for it to go should I just make it raycast in a direction away from the player and go to the hit point if there’s nothing in the way?what if the enemy is a corner?
  • should I make it so the enemy has a short range attack and have the enemy attack until it dies?
  • how do games typically approach this problem?

Do you use a simplification layer for your AI? For example a FiniteStateMachine or a BehaviorTree?
These systems allow you to conditionally change behavior.
I found myself in the same situation and decided to setup my AI with a BehaviorTree.
I chose a BT because it has an easy to understand decision path.

To elaborate BehaviourTrees a little more…
after the AI makes a choice (e.g. “close for ranged weapon”)
You can just go to a new branch to make new decisions (“should the AI run away/should the AI go to CloseCombat”).

Finite State Machines (as known from Animator) could also be a solution.
However, sloppy FSMs can quickly become very confusing and are a bit more difficult to debug.

There are some very feature-rich and easy to use assets available on the Assetstore.