Hi, I want to spawn multiple actors at random locations on the same z “plane”. Each actor needs to be spawned at random intervals (not all at the same time in a loop or something).
But they need to be at least a minimum distance away from each other (not too close to one another, or not overlapping each other).
How do you do that in UE5? Preferably without While Loop or collision spheres?
You could use the environment query system (EQS) to generate a pathing grid around the player and use random results from it to spawn things. Teh EQS grid can be told how close together the points can be, and if feeling fancy, you can add tests like line of sight to avoid enemies appearing out of thin air. I’ll try to make an example and post code noodle pics when I get home in a couple of hours.
Delays are usually terrible, so ignore that. I just wanted to be sure the level fully loaded and stuff. The filter spawns function is quick and dirty. There are better ways to do it, but:
For the filter function, it would probably be better to just shuffle the incoming array and resize it down to the desired length instead of manually adding vectors to a new array in a sequence lol. May also want to step through the full results from eqs in a foreach loop and nuke all the candidates that have a 0 score.
of course you would need to limit max iterations as well, or the loop potentialy can go infinity
This would give you irregularly disributed points, that is literally random. Depending on goal that may be more desireable than a strict grid.
If you don’t care about randomness, you may also just a hardcode a list of points you need.
If you do need a grid, then just do something like this:
Vector GenerateSpawnPoint(int Index)
{
const int Width = 10;
const int Height = 20;
const int GridStep = 100;
const float Z = 123;
if(Index >= Width * Height)
return fail;
return Vector(
(Index / Width) * GridStep,
(Index % Width) * GridStep,
Z
);
}
In a vacuum, yea, eqs is overkill. But depending on the rest of the game’s design, it could be expanded to do other stuff. Like, if one was making a Left4Dead-ish game, I could see using an invisible “director” pawn that follows the player around and decides what and when to spawn stuff. Like, if it had damage sense perception, it could decide the player needs more challenge if they haven’t been hit recently. And eqs could find hiding spots for special mobs.
Back to my filter function above. I couldn’t sleep so I stripped out the wonky sequence and stuck in a shuffle/resize combo. Also aded an input to the func to set how many things to spawn.