Spreading AI that are lining up for game defficulty

so for the last week i have been trying to make something work and i just gave up on it. i have a bunch of enemies spawning as waves and they follow you and if they touch you, you lose a life. pretty simple stuff, but when you are fighting them they eventually end up behind each other in a straight line, and i dont want that because it would make it so easy to kill them by shooting.

so i am trying to make the enemies detect when another enemy is behind them so that they kind of spread out a bit.

AAAAND…I’LL admit it i have absolutly to clue of how to go about this.

first off i started looking for assets that would make it easier but can’t seem to find any.

i have tried using raycasts but its tough to make it work

tried using empty objects having a trigger collider on it to detect using OnTriggerEnter()
but had lots of problems with it.

my final try was to use the ‘’ vector3.dot ‘’ but i cant seem to understand it.

i want any solution, maybe using the dot product to detect if the enemies are aligned on one axis (z)
litrally any solutiong would help, asset, script, anything…

also is there a way to use the navmeshagent.move() function to move the agent in local space.

One simple way is to “assign” designated position for the AI to move towards that are not exactly aimed at the player.

Imagine if the position that the AI wants to go is not where the player is at but at some offset relative to player.

Consider the following:

2 AIs chasing the user. One AI chooses to follow the player with an offset of (10,0,0) (left) and the other with offset of (-10,0,0). How would it look? Since there are only 2 AI’s, they may look a bit dumb since they are not exactly charging at the player. But with more AIs, they may seem plausible with the spacing they can have between each other. In this context, spacing by offset can be scaled with the number of AIs chasing the player. Or within certain radius which you may need to test to find the “fitting” value.

Only when the AI is near enough, then the AI tries to chase exactly at the player.

Perhaps you should look up Boids algorithm which is a much better model should you prefer. Or just by searching for “flocking algorithms” may give you what you need.