Random location to spawn objects

I have a landscape that I made and have placed foliage everywhere, what I’m now trying to do is place houses in random locations on the landscape. I would like to specify general locations where they would be but not have to place a bunch of spawners (I would like it to be pretty random). I was thinking of maybe making a volume and generating a grid from the bounds of it, save all the grid locations to a datatable. I could then turn off grid locations where I wouldn’t want stuff to spawn (this way I could have it so stuff doesn’t spawn at the very tip of a mountain or in a river, etc). I’m not sure if this is the best way (or easiest way) to do this.

Does anyone have any suggestions or maybe another way I could do this?

Bumping up

@franktech
I think I see what you are saying. So, grab the bounds of my landscape, do a linetrace from the sky, if it doesn’t hit landscape, try again. Calculating random vectors and doing a trace for each one, wouldn’t that take a lot of cpu time if it cant find anything? Would it be better to do a grid trace (so start at x0, y0 fire a trace, increase the x, fire a trace, etc until you hit the end and go down a line, etc) and save it into a datatable or something? The datatable might be huge, but then you could log if the trace hit, and where it hit, plus then you would have instant data to work with and would only have to be rebuilt if you change stuff on the landscape. But this is just me trying to think ahead.

I think a sphere trace would be a lot better than a simple linetrace.

Indeed, I have been thinking about this for a week, I just want a process down instead of going in circles. :stuck_out_tongue:

If I do the grid layout, I could paint a sacrificial phys/material where I want stuff to spawn (or not to spawn), run the trace over the entire map and log the hit locations (if it hits the material) to a file, then I don’t have to do it again unless I change the terrain/paint more locations. I will only have one type of object, and they will be spaced from each other by a large margin. I want this as simple as I can get, but its already pretty complicated.

Sphere trace would indeed be better, I meant just using a trace in general.

Dunno if this would work for your situation BUT you could just place like target points manually on your map where you would like to spawn houses and then at run-time get all target points, and have a function that loops through a random number of times to a random index in the array of points and pulls out the location of the target point and spawns a house at that location. It would be more manual work placing the points but you could ensure that you would have the room for a house at each position and it saves you from the complexity of traces and such to determine an appropriate location.