In my game, I have a group of AI’s that need to stay within a set distance from their leader. To do this, I originally tried a method in which the AI’s would check the distance from their leader, if it was less than their max distance away, then they would continue what they were doing, else they would turn around, look directly at the leader, then walk forward till they got near him. The problem was that it just didn’t look natural with all the AI’s turning around and walking right next to the leader and then walking away every 10 seconds. So I was wondering, how would you make the AI’s stay within a set distance from their leader?
Simplest solution : Make the radius larger.
That at make it so that they just have to head back to a specific radius of the leader rather than all the way back to their side.
The real solution is to make your agents smarter, have them pick tasks based on proximity to the leader. Rather than run to the leaders side which is pretty useless an action have them pick a fight/task that’s closer to the leader to run to.
What you can do is rather than use a max radius (except once they are out of visible sight in which case you want them to return to the leader no matter what usually), weight the available tasks, including the one that the agent is currently preoccupied with, if you weight them by distance from leader + danger to self (i.e. I’m being attacked i need to defend myself) then pick the task that’s highest value, re-evaluate the weights and tasks maybe once every few seconds while currently active in a task and away your agents should go. This will cause them to run closer to the leader as the distance gets greater and the nearest task drops in weight compared to closer tasks.
You can then start to make your agents smarter, maybe have them cover your leaders 65 a little more by reducing the weight of foes directly in the line of sight for the leader etc, you can build up a series of “traits” based on certain factors to make them more heroic or more self serving by adding multipliers to these weight values, maybe have them more or less sneaky by picking tasks along paths that keep them out of line of sight of foes etc. Have them pick tasks based on their class strengths, e.g. weight foes with weaknesses to your agents weaponry higher than others, or even make them work as a team, so weight foes that are already under attack (or recently hit, or have low hp) as higher priority targets. It’s how you weight and evaluate the next task that will sell the agents as both being useful and believable. Of course your game may have no combat at all, but the same rules should apply unless the agents are meant to be robotic proximity sensing drones, in which case just follow the first advice.