Procedural generated Cube with different heights

Hey everyone,

I’m just a starter in Unity. For a university project I want to generate cubes with random height at the position where the mouse is while the camera is flying straight on the Z axis.

Right know a made a reycast script who give me the position of the mouse back. But now I’m a bit stumped how to go on.
I know I need a script to
a) generate the cubes with random heights like (Y between 1 and 5)
b) definite the areas where the cubes are allowed to spawn, so they don’t overlap and have space to each other.

Has anyone experience or knows a good way to solve this problem or maybe I miss something important.
Thanks for any help or food for thoughts.

greeting tited

So, you need a random number?

int r = Random.Range(1, 6); // upper bound is exclusive
// you can just apply this to the cube's y scale/localScale.

As for the area to spawn, this might be worth a shot: Unity - Scripting API: Bounds.Intersects
Create a bounds, based on your dimensions where you raycast for your target placement, and if no intersection is reported, no other bounding box is in that spot. If you wanted additional space, you could add that to your imaginary bounds, maybe.
:slight_smile: