how do i generate cubes to form a sphere?

im making a game like minecraft and i have a script that generates cubes for a floor but i cant fix it so that the cubes size are 0.005 and im trying to make them generate a sphere out of cubes kinda like world edit on minecraft any help will be appreciated

for (var y = -4; y < 5; ++y)
for (var x = -4; x < 5; ++x) {
    var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.position = transform.position + Vector3(x, 0, y);

A simple (but somewhat stupid) way to create a hollow sphere with axis aligned blocks is to generate coordinates for a cube with the same sphere size, but only instantiate the ones that are at the radius distance:

function CreateSphere(center: Vector3, blockSize: float, radius: float){
  var qBlocks: int = Mathf.Ceil(radius/blockSize); // how many blocks fit in the radius
  var pos: Vector3;
  for (var ix: int =-qBlocks; ix<=qBlocks; ix++){
    pos.x = ix * blockSize; // calculate pos.x...
    for (var iy: int =-qBlocks; iy<=qBlocks; iy++){ //
      pos.y = iy * blockSize; // calculate pos.y...
      for (var iz: int =-qBlocks; iz<=qBlocks; iz++){
        pos.z = iz * blockSize; // calculate pos.z...
        if (Mathf.Abs(pos.magnitude - radius) <= blockSize/2){ // if block at sphere surface...
          // create it:
          var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
          cube.transform.localScale = * blockSize; // set scale at once
          cube.transform.position = center + pos; // place the cube at calculated pos
      } // end of z loop
    } // end of y loop
    yield; // let Unity breath till next frame
  } // end of x loop

function Start () {
  // create sphere at this object position:
  CreateSphere(transform.position, 0.05, 1);

The yield instruction added after the y loop is intended to not block Unity: each YZ wall will be instantiated each frame.

If you need a solid sphere, replace the if clause to:

  if (pos.magnitude <= radius){ // if block inside radius...

But solid spheres take a lot of time to be created, and another big time to be rendered - the framerate drops like a rock when the sphere enters the camera frustum.

As @Bunny83 said, the enormous amount of game objects generated for a simple sphere is a big performance killer! Depending on what you want to do, creating a procedural mesh (like @Berenger suggested) with the algorithm above (or another less stupid one) may be the right solution.

I suggest you take a look at how a sphere’s mesh is procedurally created in 3D modelling programs, as what you want is quite close.

A quick google search gave me those :

Spherical brush will create a ball of blocks specified with [id:meta], of size defined with radius and minecraft circle . As diameter is radius times two and the minimum diameter of a brush is 1, [radius] of 3 will give you a seven blocks wide ball. In addition to [radius], cylinder brush also accepts a height value (If you don’t define a height, you’ll get a one block thick disc).

So, “//br cyl 35:0 3 4” will create a 7 blocks wide, 4 blocks tall cylinder of white wool, with the center of the bottom layer on the block where you click with the brush.


You can’t use seperate gameobjects for each cube. That will kill your performance. You should take a look at the minecraft starter kit. Minecraft is one of the most complex games that have been released recently. It uses very advanced technologies. It’s easier to create a new Crysis or BioShock like game from a programming point of view. You should have really good knowledge of math, algorithms and how the graphics hardware works, otherwise you won’t get very far…