Since my question (although vague) was how to choose a random point on the perimeter of a 2D box, @eldany.uy was the first to point me in the right direction. This is how I ended up implemented in C++. I’m kind of embarrassed I didn’t think of this before… but I’m glad there was also a discussion about how to do it in 3D. Thanks everyone!
SpawnLocation = BoxBounds.Origin;
// Y is left-right Z is up-down
const float OffsetX = 0.f;
float OffsetY;
float OffsetZ;
if (const int32 RandomDirection = UKismetMathLibrary::RandomIntegerInRange(0, 3);
RandomDirection == 0)
{
// top
OffsetY = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Y, ScaledBoxBounds.Y);
OffsetZ = ScaledBoxBounds.Z;
}
else if (RandomDirection == 1)
{
// right
OffsetY = ScaledBoxBounds.Y;
OffsetZ = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Z, ScaledBoxBounds.Z);
}
else if (RandomDirection == 2)
{
// left
OffsetY = -ScaledBoxBounds.Y;
OffsetZ = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Z, ScaledBoxBounds.Z);
}
else
{
// bottom
OffsetY = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Y, ScaledBoxBounds.Y);
OffsetZ = -ScaledBoxBounds.Z;
}
FVector Offset = { OffsetX, OffsetY, OffsetZ };
SpawnLocation = FirstSpawnLocation + Offset;