Instantiate object in random spot in a complex 3D space

I have a city scenario and I’d like to create a script that chooses a random spot in map and places an object there… when the spot is land or sand I dont see any problem but I dont know how to deal with buildings and trees for example… I could do a boxcast to their meshColliders from top but then there would be places I would not find, like under a tree branch for example…or under an umbrella that’s part of a building…
How could I approach this? is there any technique or algorithm model I could search for?

To illustrate better my case… I want to spawn easter eggs inside of a city, thats why sometimes I neeed them to be hidden between the meshes of certain models like buildings or trees…

Thanks in advance!

I know you’re hoping for an automated solution but you really should instead author the locations where things go in a case like this. It just makes it much more interesting.

If you ever played GTA, you’d know why. Their spawns are authored in “very cool” places.

One way to author locations is with invisible GameObjects containing a script that reports their position to the object placement script, saying “I am a place you could use.”

For instance you might make 100 different locations and the spawner would choose randomly from those.

1 Like

Yeah you know what, you’re right… yesterday night I came to the same conclusion, the only ‘matter’ is this will be a forever task since our intention is expand the city with new building types but well, not a biggie…

Thanks dude

PS: im more a Yakuza guy, but gotcha ;D

if you use building prefabs or road pieces, could have the spawn spots on those ready defined.

like if building has table, then there is one spot under it, or at front door, or at side of the building…
and maybe adjust probability for those, so that less objects spawn at front door for example (if it looks weird in game that almost every doorstep has object)

2 Likes

I was also thinking into creating different spot categories somehow… since I might use certain locations to spawn little things like… eggs or boxes (under a chair ) but I might want to spawn other stuff in the future that’s bigger and won’t fit in some places… so maybe it would be useful to bind some sort of parameters to those spots like… size… height… reachable by a human…

This is smart. My Jetpack Kurt game uses a GenericSiteMarker script that contains a few parameters I can set such as confined-ness, or if the emplacement should cast-to-ground and tilt-to-ground (eg, make a false flat base or just use the natural terrain to put the landing marker).

Yeah I like the idea but I’m not sure if I want to add 1 script per spot, I was thinking to keep it in a ScriptableObject… maybe bind item-id to list of spots with their parameters.

1 Like