- Home /
How to generate a grid for procedurally generated platforms
I am attempting to create a number of procedurally generated floating platforms which can have a number of objects placed on top of them, and want to use a grid-based system for doing so, however, I have no idea how to go about generating said grid to vaguely represent the following image.
As shown I want to get a grid that prevents objects being spawned too close to the edge so that they don't overhang.
Answer by pako · Feb 24, 2019 at 12:24 PM
In you scene, create empty GameObjects at the center of each square in the grid, at the exact position that you want to spawn your objects. Let's call these empty GameObjects Spawn Points, and we'll just use their transforms as reference for spawning the objects. So, for ease of use rename each empty GameObject as "SpawnPoint".
Also for ease of use, create another empty GameObject in the scene and rename it something like "GridRoot". Then select all SpawnPoint GameObjects and drag-n-drop them to the GridRoot GameObject.
There are many different ways that you can use the above Transforms, so I'll just give an example bellow.
Create a C# script and name it something like "Grid".
In this script declare a List:
public List<Transform> SpawnPointTransforms;
Now, add the Grid component (script) to the GridRoot GameObject.
In the Inspector set the size of the List to, say 46, per your example image, and drag-n-drop each SpawnPoin GameObject to corresponding List elements in the Inspector.
After the above is set up, you can drag-n-drop the GridRoot GameObject to your Platform GameObject, and make it its child. Adjust the position of GridRoot so that it aligns with the Platform as you want it.
Finally, create a prefab of the complete Platform, so that you can reuse it as necessary.
The are many possible ways of using this, but the above gives you as starting point for laying out the spawn points and getting access to them in script.
@cwells1298 if my answer has helped you, I'd really appreciate if you accepted my answer by clicking on the checkmark (just below the up/down vote on the left side of the answer). We'll both get reputation points if you do :-)
Your answer
Follow this Question
Related Questions
What is wrong with this mesh editing code? 0 Answers
How do I go about texturing a flat-shaded generated mesh? 1 Answer
No Procedural Terrain without lags? 2 Answers
Terrain detail meshes/trees via script? 2 Answers
Mesh deformation by terrain shape 0 Answers