Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by kernes · Jun 07, 2021 at 10:27 AM · rotationrubikscube

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:

RubiksCube bug photo


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 :)

screenshot-2021-06-06-221153.png (123.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kernes · Jun 07, 2021 at 03:28 PM 0
Share

@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!

avatar image Bunny83 kernes · Jun 07, 2021 at 05:10 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges