- Home /
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;
}
}
I found another oddity. I tried to assign the cubes to "rightPivot" directly, one at a time without the foreach loop and discovered that order in the array does not correspond to the order in the Hierarchy. Calling cube[0] gives cube[24], calling cube[1] gives cube[8], etc. I don't see any obvious pattern to it. As with the other questions I'm finding I'm not sure if it is effecting my main issue or not, but would love an explanation as to why this is happening.
Thanks
The "second oddity" might be explained by this ?
The code you are showing clearly shows you create just one "endRotation" GameObject, so if you are getting 27 of them, this script is clearly attached to more than 1 GameObject and that's why everything happens multiple times. Do you perhaps attach this script to 1 Cube in the scene and the clone that cube to make the other 26 ? The code you show here doesn't give any clue about how you create the whole setup.
27 = 3*9 and 243 = 9*27 so there's clearly a pattern that indicates some unintentional recursion.
Nose$$anonymous$$ills!
Thanks! You are right I had my script attached to every cube. I left it attached to just 1 and it works. The rotation stops at 90 degrees and I only get 1 instance of "endRotation" and "rightPivot". Also, because of that fix the looping/counting variable issue is no longer applicable. I'm not sure why this was having the particular affect that it was, but it is resolved.
The only thing left is that my cubes[] array is still taking the cubes out of order. I would love to know why that is the case, but for now it doesn't seem to be having an adverse effect.
Thank you again!
Answer by DiegoSLTS · Aug 02, 2015 at 04:41 PM
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: http://docs.unity3d.com/ScriptReference/Mathf.Epsilon.html
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.
Thanks DiegoSLTS!
Very helpful. $$anonymous$$y cube is working great now.
Answer by thelasthope313313 · Jun 15, 2017 at 11:55 PM
hi i cant access to unity doc cause of sanction what should i do can sb send the docs to my mail?
Your answer
Follow this Question
Related Questions
Need help on the 3ds max style camera control 0 Answers
Gyro calibration in android? 0 Answers
Moving an object in a circle towards joystick 0 Answers
how to input words from array to GUI? 0 Answers
Rotate Vector3 array around a point 1 Answer