- Home /
How could I make a Randomly Generated Grid-Based Neighborhood?
Hi, I'm working on a game, where the player picks dumps trash from Trash cans into a Trash Truck. The game's world itself is going to be a Low-Poly, Grid Neighborhood. I'd like each property to have a randomly generated layout for a house (random shape, random color, etc., etc..,), and with randomly generated plants, a surrounding fence, trees, among other things scattered around each property. I'd like this to be customizable, so that I can maybe specify the randomness / frequency of said items occurring, and what that item is, etc.. I'm probably going to try to make my game have multiplayer, which I've never tried before, so I don't know if this will be an issue or not, but I'd like it to be so that the random generation of the world won't be different for other people playing, I'd like them all to have the same randomly generated world. If possible, I'd also like to make the grid world itself expandable, so that I can maybe make some differently-layed-out levels.
Here is essentially what one of my Grid-World Tiles/Sections look like (I also have them highlighted for reference,),:
I essentially just want the part in Green to be where the properties are.
Is this possible? If so, how could I go about doing this?
If anyone needs any more information, please, let me know!
Thanks!
Answer by Llama_w_2Ls · Dec 09, 2020 at 07:42 AM
Most game developers, when working with procedurally generating villages or neighbourhoods, use pre-made prefabs of the different possible houses and randomly scatter them along the grid, as long as the ground/terrain of that area is flat enough.
Ok, thanks, how would I go about doing the scattering? Also, luckily, my ground is literally that flat, green, plane.
If your grid is made up of square-like coordinates, you can always use the middle square as the spawn point for any houses, then drop down a sidewalk that starts from the door of the house into the road.
To scatter trees everywhere, you could have a random number generator, and if the random number exceeds a threshold (having a threshold allows you to edit it in the inspector as a randomness value), place it in an allocated square that isn't the square of the house or sidewalk. Unfortunately, you may have to pre-deter$$anonymous$$e which squares aren't viable for a tree to be placed there.
Sorry, what exactly do you mean by I might have to pre-deter$$anonymous$$e which squares aren't viable? Also, as I said with the reply below, my other reply, I've been following that one video. Here's the script I have currently: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RandomGen : $$anonymous$$onoBehaviour
{
public GameObject itemToSpread;
public int numItemsToSpawn = 10;
public float itemXSpread = 10;
public float itemYSpread = 0;
public float itemZSpread = 10;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < numItemsToSpawn; i++)
{
SpreadItem();
}
}
void SpreadItem()
{
Vector3 randPosition = new Vector3(Random.Range(-itemXSpread, itemXSpread), Random.Range(-itemYSpread, itemYSpread), Random.Range(-itemZSpread, itemZSpread)) + transform.position;
GameObject clone = Instantiate(itemToSpread, randPosition, Quaternion.identity);
}
}
So, maybe, is there a way, to, maybe, say, have a certain Tag or Layer for these objects that should not allow spawning on top of them? Or maybe, a way to detect if it's within the mesh, and then offset it to be outside of it?
Also, how could I go about making it so that the random objects won't spawn on top of each other? And maybe be inset back from the perimeter of all the other GameObjects? (For Example, there'll be a house in the middle of each ground plane, and it will have a sidewalk that connects to the road. There will be trees everywhere else, in this example, at least. I don't want the trees to spawn in or on the house or sidewalk. If possible, I'd like to be able to specify the offset from the edge of the house and sidewalk, and the edge of the ground plane itself, how far back in the Trees start spawning, for example.). Is this possible?
I've currently found this video, and I was also wondering, is there a way to change the randomization / update the amount of objects in the inspector at runtime? Right now, it seems like I have to do this in the Editor, then Run the Scene.
Thanks.
I'm not entirely sure. You could have a list of possible squares to place an object on, and if an object is placed in a square, remove that square from the list, as to not allow objects to spawn inside or on top of one another. Sorry, I hope you figure it out.
Ok, thanks. It's ok, this is helpful, thanks! Oh, one other thing, what do you mean by threshold?
Answer by CmdrZin · Dec 09, 2020 at 12:08 AM
You can use Random.seed to generate the same sequence of random values used for scene construction.
see https://docs.unity3d.com/530/Documentation/ScriptReference/Random-seed.html
As for your lots, you could layout a Plane then layer smaller Child Planes on it for the possible positions of various lot items (House, fences, shrubs, etc.) and populate those by randomly selecting from lists of prefab items.