# Rotation of multiple cubes!

Hello there!

I have one big problem, and i can't solve it, i never work with 3D before, i come from cocos2D iPhone and Actionscript 3 Flash! So, the things i need help over there can be realy simple to someone to know 3D, but its killing me!

I have one big cube with many cubes inside then, like that:

To create then i make this code:

``````void createSphere ()
{

float startX = -2.5f;
float startY = -2.5f;
float startZ = 8f;

for(int y = 0; y < 6 ; y++)
{
for (int z = 0; z < 6; z++)
{
for ( int x = 0; x < 6; x++)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

cube.transform.position = new Vector3(x+(0.1f*x)+startX,y+(0.1f*y)+startY z+(0.1f*z)+startZ);

int rand = Random.Range(1,4);

switch(rand)
{
case 1:
cube.transform.renderer.material.color = Color.red;
break;
case 2:
cube.transform.renderer.material.color = Color.blue;
break;
case 3:
cube.transform.renderer.material.color = Color.green;
break;
}

}
}
}
}

``````

What i want to do, is detect when use make a slice over iPhone screen! And make the all cube rotate around itself:

Detect the side of slide:

``````if(Input.GetMouseButtonDown(0))
{
lastMousePos    = Input.mousePosition;
actualMousePos  = Input.mousePosition;
press           = true;
}
if(press)
{

if(actualMousePos.x > (lastMousePos.x + 20f))
{
cubesHolder.transform.RotateAround(Vector3.up, 0.05f);
}
else if(actualMousePos.x < (lastMousePos.x - 20f))
{
cubesHolder.transform.RotateAround(-Vector3.up, 0.05f);
}

lastMousePos = Input.mousePosition;
}
if(Input.GetMouseButtonUp(0))
{
lastMousePos    = new Vector2();
actualMousePos  = new Vector2();
press           = false;
}

``````

But he cube start loose himself with some rotations, make sense with 3D, but what is the best way to make this work?

i upload the unity3D file here:

try.html">http://www.2shared.com/file/u7nJbJhW/Unity_try.html

Attach this script to any GameObject. It will create the cubes and make them children of the GameObject. I changed the script so the number of cubes and spacing is changeable.

``````using UnityEngine;
public class Cube : MonoBehaviour {
Vector2 lastMousePos, actualMousePos;
bool press = false;
public GameObject cubesHolder;
public Vector3 nCubes = new Vector3(6f, 6f, 6f);
public Vector3 spaceBetweenCubes = new Vector3(1f, 1f, 1f);
private Vector3 cubeSize;
private Vector3 cubeHalfSize;
private Color[] colors;

void Start () {
colors = new Color[3] { Color.red, Color.blue, Color.green };
createLotsOfCubes();
}

void Update () {
if(Input.GetMouseButtonDown(0))
{
lastMousePos = Input.mousePosition;
actualMousePos = Input.mousePosition;
press = true;
}
if(press)
{
lastMousePos = Input.mousePosition;
if(actualMousePos.x > (lastMousePos.x + 20f))
transform.Rotate(Vector3.up, 0.5f);
else if(actualMousePos.x < (lastMousePos.x - 20f))
transform.Rotate(-Vector3.up, 0.5f);
}
if(Input.GetMouseButtonUp(0))
{
lastMousePos = Vector2.zero;
actualMousePos = Vector2.zero;
press = false;
}
}

void createLotsOfCubes ()
{
// Actual renderer size
GameObject tmpCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubeSize = tmpCube.renderer.bounds.size;
cubeHalfSize = cubeSize/2f;
DestroyImmediate(tmpCube);

// There are nCubes - (1,1,1) spaces between the cubes so the total size is
Vector3 figureSize = new Vector3( nCubes.x * cubeSize.x + (nCubes.x - 1f) * spaceBetweenCubes.x,
nCubes.y * cubeSize.y + (nCubes.y - 1f) * spaceBetweenCubes.y,
nCubes.z * cubeSize.z + (nCubes.z - 1f) * spaceBetweenCubes.z );
// Half of the size of the entire set of cubes
Vector3 figureHalfSize = figureSize / 2.0f;

for(int y = 0; y < nCubes.x ; y++)
{
for (int z = 0; z < nCubes.y ; z++)
{
for ( int x = 0; x < nCubes.z ; x++)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
// Set this object as the parent object
cube.transform.parent = transform;
// The position is : -extent + size of cube + spacing + offset to center (half size of cube)
cube.transform.position = new Vector3( -figureHalfSize.x + x*(spaceBetweenCubes.x + cubeSize.x) + cubeHalfSize.x,
-figureHalfSize.y + y*(spaceBetweenCubes.y + cubeSize.y) + cubeHalfSize.y,
-figureHalfSize.z + z*(spaceBetweenCubes.z + cubeSize.z) + cubeHalfSize.z );
// Change the color
cube.transform.renderer.material.color = colors[Random.Range(0, 3)];
}
}
}
}
}

``````