- Home /
Rotation of Rubiks Cube side
Hello, I have a problem with my Rubiks cube application. I created a 3x3x3 Rubiks cube model in Blender, then I imported it to Unity. I write methods to rotate each side by pressing a specified key on the keyboard.
And everything is fine if I rotate only one side. When I am trying to rotate for example red face (i call the face by the colour of the centre element - it always stays in the same face/position) and the white face, everything looks weird. Here is an example:
I am pretty sure that this is something related to coordinates and scale. But I don't understand some things. I set the coordinates of elements to int values (the same with face pivots). But somehow rotation changes their position and scale (eg, y=-9.536743e-07 or scale z=1.000001).
Here is my code. I have an analogical method for each face. I create a parent object and assign to that object children (small cubes) with correct coordinates. I think, that it will work if the coordinates will be correct after rotation. But they don't.
void RotateWhite()
{
face = new GameObject[9];
i = 0;
whitePivot = new GameObject("whitePivot");
whitePivot.transform.position = new Vector3(0, 4, 0);
print(whitePivot.transform.position);
whitePivot.transform.parent = this.transform;
// add children to face parent to rotate cubes with y coord eq 4
foreach (GameObject cube in cubes)
{
if (cube.transform.position.y == 4)
{
cube.transform.SetParent(whitePivot.transform, true);
face[i] = cube;
i++;
}
}
// rotate by 90 deg
whitePivot.transform.Rotate(0, 90, 0);
// set scale to 1 (i tried to prevent 1.000001 like scale problem - didn't works)
whitePivot.transform.localScale = new Vector3(1, 1, 1);
// revert previous parent
foreach (GameObject cube in cubes)
{
if (cube.transform.position.y == 4)
{
cube.transform.SetParent(transform, true);
}
}
// delete temporary object
Destroy(whitePivot);
}
I am new to unity and I was done my best to fix this problem on my own but I run out of ideas. I tried to truncate the decimal part of coordinates and scale but sadly, with no results. I will be very thankful if someone will help me with this problem :)
Answer by Bunny83 · Jun 07, 2021 at 08:35 AM
You may want to have a look at this question over here. The script I wrote generates it's own rubik's cube mesh in Start which is simply used for each subcube. Also the "cubes" array has a specific structure (see the comment inside the code). Also note that the script uses a default Unity unit cube and assumes the pivot of each subcube to be in the center and each subcube has a side length of 1. Note since the logic is literally the same for every layer, you need to have 27 gameobjects instead of only the 26 visible ones. So you need one in the center as well for rotating the middle layers. Though this could be just an empty gameobject.
Keep in mind that when you want to use my script, you have to do some modifications in order to work with premade subcubes. You can actually ditch the CreateRubikMesh method and also clean up the Start method.
Even if you don't use my script, you may want exam it (it's just 300 lines) to better understand the complexity involved. Your approach of comparing the absolute worldspace position to a fix value won't work. If you want to stick to your approach, you can try checking for the position being greater than / smaller than a certain threshold. However keep in mind when comparing absolute worldspace positions you can never rotate the cube itself freely. You usually want to use localPosition for such things and have the cubes being grouped in a parent object.
Since you said you don't understand those lightly off fractional part, I highly recommend to watch this Computerphile video on floating point numbers.
@Bunny83 Your response is very helpful! Especially that question that you linked and Your rubiks cube script. I think that Your approach is simpler than my so I will use Your script. Thanks!
Feel free to use it ^^. btw: Your model looks really nice. It's fascinating what some gaps and slightly rounded corners do to the overall look. Currently I only have a tiny rubik's cube on my desk which I always solve when I'm bored ^^. Though I think I should look out for my normal sized one. I just can't find it at the moment :)
Your answer
Follow this Question
Related Questions
Rotating the cube with Quaternion.Lerp 1 Answer
Flip over an object (smooth transition) 3 Answers
Rotation of multiple cubes! 1 Answer
Rubik's Cube, grouping and rotating problems 2 Answers
Unity Simulate Local Rotation 0 Answers