Help understand some things in Unity rotation!

Hey guys, how are you? I starting play with unity yesterday, i come from cocos2d / Actionscript 3! Using C# to program, i start a little project just to know the platform and thing i can do, but something just don’t make sense to me!

Lets start with something simple:
I create a bunch of cubes make one big cube like these:

This was created by the following code:

void createSphere ()
	{
		
		float mainDelay = 0f;
		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, 15+startY, z+(0.1f*z)+startZ);
		            
		            float targetY = y+(0.1f*y)+startY;
		            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;
		            }
					mainDelay += 0.2f; 
		            
					iTween.MoveTo(cube, iTween.Hash("y", targetY, "time", 0.2, "easeType", "bounce", "delay", mainDelay/10));
		        }
			 }
	    }
	}

Ok this was easy, now i want to rotate de cube when the user interact, the first problem! I can’t findo some object to hold all cubes itself, like a MovieClip (AS3) or a Layer (cocos2d).

Searching around the web i found this:

GameObject cubesHolder 	= new GameObject("cubesHolder");
cube.transform.parent = cubesHolder.transform;

Send each cube inside this object the Hierarchy star absolute right, exactly what i want:

But this just hold everything together on the Hierarchy, if i move the cube, when things are falling, they lost and things starting falling in the wrong place:

This just don’t make sense, since the positions are relative to the world!!

How i can create a object inside another? And this stay inside this object?

And how i can make the cube rotate around himself like this:

Like if i moving the yellow like?

nobody?

I am guessing you are making a “Rubik’s Cube” game right?

If you want to rotate the whole cube you simply need to rotate the parent object. You can do this in script like this:

cubesHolder.transform.RotateAround(Vector3.up, 90);

This would rotate the cubeHolder 90 degrees around the World Y-axis (corresponding to Vector3.up, which is a constant vector always pointing up). Or you could rotate along a relative axis:

cubesHolder.transform.RotateAround(cubesHolder.transform.up, 90);

That would rotate the cubeHolder around it’s own LOCAL y-axis.

If you want to rotate a set of cubes first get all the cubes you need to rotate and put them in an array. This you’ll have to figure out yourself because I don’t want to bother thinking about it. It shouldn’t be difficult.Then:

foreach (GameObject cube in cubesToMove) {
cube.transform.RotateAround(cubesHolder.transform.position, rotationAxis, rotationIncrement);
}

where rotationAxis is a LOCAL axis of the cubesHolder (ie: cubesHolder.transform.up) and rotationIncrement is how far you want to rotate it. I don’t know how to achieve this with iTween as I haven’t used it yet. My guess is that it has a method similar to RotateAround. Note that the RotateAround method has two overrides, if you omit the first Vector3 (which indicates the point to rotate around) then you will be rotating around the local origin.

Hey dude!

this keep don’t working at my try =/
When i try to move my cube around it self, they keep rotating around wrong pivot:

Look’s the pivot of the main cubeHolders its always the camera!

The code:

using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {
	
	
	bool press 				= false;
	Vector2 lastMousePos 	= new Vector2();
	Vector2 actualMousePos 	= new Vector2();
	GameObject cubesHolder;
	
	GameObject actualGameObj;
	GameObject[] allGameObjs;
	
		
	public Light DirectLight;
	
	// Use this for initialization
	void Start () 
	{
		createSphere();
	}
	void createSphere ()
	{
		
		allGameObjs = new GameObject[6*6*6];
		
		float mainDelay = 0f;
		float startX = -2.5f;
		float startY = -2.5f;
		float startZ = 8f;
		int counter = 0;
		
		cubesHolder 	= new GameObject("cubesHolder");
		
		
		
		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.parent = cubesHolder.transform;
		            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;
		            }
					
					allGameObjs[counter] = cube;
		            
					
					counter++;
		        }
			 }
	    }
	}

	// Update is called once per frame
	
	void checkIfSomeCubeUnder()
	{
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		
		
		if (Physics.Raycast(ray, out hit)) 
		{
			string tryName = hit.transform.name.Substring(0, 4);
			if(tryName == "cubo")
			{
				//Want to the the FACE the ray hit the cube, if is UP/DOWN, LEFT/RIGHT, FRONT/BACK
			}
		}
	}
	
	void Update () 
	{
		if(Input.GetMouseButtonDown(0))
		{
			checkIfSomeCubeUnder();
			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;
		}
    }
}

I can’t understand how the unity work with this, check the example i attached if you can:
This don’t make any sens, looks the pivot when you create things dynamically you can’t change anything!

476469–16728–$Unity _try.zip (1.87 MB)

Nothing?