Rubik's Cube, grouping and rotating problems

Hi,

I’m making a Rubik’s Cube type of situation with 3x3x3 cubes. I’ve made some progress, but I’ve run into several questions. At this point I have been able to successfully gather nine cubes and rotate them smoothly 90 degrees, but only if the function that gathers them is called from the Start function. When I try to call it from an input its jumpy and spins farther than 90 degrees. When testing my code on a single cube not requiring the foreach loop it works fine. In the course of trying to figure it out I have come across several things I can’t explain that may be a part of the issue. Either way I’d love to know why those things are happening as well.

The first, when my “endRotation” and “rightPivot” variables are instantiated Unity makes 27 of them, one for every cube, rather than just the 1 I need. Second, when I have tried to apply a counting variable, “i”, to the foreach loop I have found that it only goes up to 9 and then starts over for a total of 243 times. Also, when I have tried to apply a second counting variable, to count every time “i” gets to 9, it never gets higher than 1.

Thanks in advance for any help.

public class ComboRotator : MonoBehaviour
{
private GameObject cubes;
private GameObject rightPivot;

private GameObject endRotation;

private bool gathered;

void Start () 
{
	rotateGo = false;
	gathered = false;

	endRotation = new GameObject ("endRotation");

	cubes = GameObject.FindGameObjectsWithTag ("Cube");
}

void Update () 
{
	if (Input.GetKeyDown(KeyCode.UpArrow))
	{
		if (gathered == false)
		{
			GatherRight ();
		}
	
		endRotation.transform.Rotate (Vector3.right, 90, Space.World);
	}
	
	rightPivot.transform.rotation = Quaternion.Lerp (rightPivot.transform.rotation, endRotation.transform.rotation, Time.deltaTime * 8);
	}
}

void GatherRight()
{
	rightPivot = new GameObject ("rightPivot");  
	rightPivot.transform.position = new Vector3 (2, 1, 1); 

	foreach (GameObject cube in cubes) 
	{
		if (cube.transform.position.x == 2) 
		{
			cube.transform.parent = rightPivot.transform;
		}
	}

	gathered = true;
}

}

This:

if (cube.transform.position.x == 2)

is a bad idea. transform.position.x is a float, and comparing floats for equality (==) is usually the source of bugs, your compiler might be showing you a warning about it too.

Like I said in your other question, float operations produce some errors inherent to float (or double) variables. When comparing against a float use <, <=, >, >= (even != is a bad idea). If you want cubes with x position “equals” to 2, write something like:

if (Mathf.Abs(cube.transform.position.x - 2f) <= Mathf.Epsilon) {
    //something
}

Epsilon is: Unity - Scripting API: Mathf.Epsilon

Anyway, you might want to do something different, maybe use int variables to keep track of where a cube is in the rubik cube, like coordinates (face, row and column), and then you can check for equality (all cubes with the same “face” value form a face). You just update the values whenever you do a rotation.

hi i cant access to unity doc cause of sanction what should i do can sb send the docs to my mail?