Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
1
Question by gemmaF · Mar 12, 2018 at 04:13 PM · rotation axisrotatearoundpivotrubikscube

How to rotate columns of a Rubik's cube?

Hi! I'm working on my master's project which is about a rubik's cube. I'm pretty new to unity and c#, I've been studying it since January and I'm facing some problems with the structure of the movements. I've find some ideas on other questions but they are not so clear since I'm a newbie. At the moment I've created my cube (I've got all my little cubes as children to an empty object set as parent). I would like each columns and rows to rotate on click but at the moment I've just managed to rotate a face setting an empty object as a rotation center of that face and making it parent of the 9 cubes forming it (inside the cube parent). This was just a test and I know it's not the right way. So, I was wondering what it would be the most rightful way to write the script in order to rotate columns and rows.

This is what I've done up to now.

sing System.Collections; using System.Collections.Generic; using UnityEngine;

public class CubeRotation : MonoBehaviour {

 public GameObject rotationFaceA;
 private float speed = 2f;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {

     rotationFaceA.transform.Rotate(0, 0, 1+90 * speed);
 }

}

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

2 Replies

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

Answer by Bunny83 · Mar 12, 2018 at 08:56 PM

You have to think about two things at the same time. First when you rotate any layer / face two things happen:

  • First of all each of the 9 sub-cubes in a layer / face get rotated 90°. So their orientation changes as well as their physcal location within the 3x3x3 cube

  • Second the logical positions of those 9 sub cubes change as well so the composition of the orthogonal layers change.

Usually it's the easiest approach to organise the 27 (26 + center) cubes in an array in a logical manner. Then you basically have 18 different rotations a user can perform (3 cardinal directions x 3 layers in each direction x 2 rotation directions). If you don't want to write out the "swapping logic" for all 18 cases, just do the 9 cases. The opposite rotation direction is just 3x the other way.


Another way would be to define an indexer (getter / setter) for each layer and just apply the same rotation algorithm to a selected layer. This would also simplifying the rotation part as it would be easier to collect all cubes that need to be rotated.


I would recommend to just use a flattended array with 27 elements. So for example element 0-8 are the bottom layer, 9-17 is the middle layer and 18-26 is the top layer. (0,1,2) (9,10,11) (18,19,20) would be the front face while (6,7,8) (15,16,17) (24,25,26) would be the back side face


I would probably use a struct like this

 public struct CubeLayer
 {
     public Transform center;
     public Transform c0,c1,c2,c3,c4,c5,c6,c7;
 }

to represent a single layer where "center" is the center piece of that layer and c0 - c7 are the surrounding pieces in clockwise order. That way you can apply a simple rotation swapping algorithm. For example a clockwise rotation would swap like this:

 c0 = source.c2;
 c1 = source.c3;
 c2 = source.c4;
 c3 = source.c5;
 c4 = source.c6;
 c5 = source.c7;
 c6 = source.c0;
 c7 = source.c1;

All you need with that struct is a get layer and set layer method that takes in a parameter what layer you want to access. You may want to use the "official notaion"

 public enum ELayer
 {
     F, // front face (around z axis)
     R, // right face (around x axis)
     U, // upper face (around y axis)
     L, // left face (around x axis)
     B, // back face (around z axis)
     D, // down face (around y axis)
     M, // middle layer around x-axis
     E, // middle layer around y-axis
     S // middle layer around z-axis
 }

Since i was a bit bored i just implemented a rubik cube script. All you need are 27 unity default cubes as a child of an empty gameobject. Attach the script to the empty parent and Just drag all the children onto the cubes array (can be done at once when you lock the inspector ). Create a new material and use the UnlitVertexColor shader. Assign the material to all cubes and you're done ^^


At runtime you can press one of the layer buttons (L R U D F B M E S) to rotate that face counter clockwise. Hold shift to rotate clockwise. This follows the common notation of capital letters for clockwise and lower case letters for counter clockwise rotations.

Example Image

Comment
Add comment · Show 3 · 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 gemmaF · Mar 13, 2018 at 11:59 AM 0
Share

Thank you very much!! This is incredibly useful, I'll definitely gonna try this method, the logic behind it seems very well explained so I can follow even if I'm still a very newbie!

avatar image tormentoarmagedoom gemmaF · Mar 13, 2018 at 12:01 PM 0
Share

Then accept the answer.....

avatar image gemmaF · Mar 27, 2018 at 08:43 AM 0
Share

Hello! This suggestion has been really useful but I have a question. Would it be possible to maintain this structure and replace the rubik's notion movements used here with a click and drag function of the mouse? Basically you would move a row by clicking on a cube and mimic the movement of dragging it up or down, with the mouse, to perform a rotation of +90 or -90. In my project, since it will be a kind of exhibition platform, I wouldn't use different colors on the cube, just one texture for all cubes, and I'd like the idea of having the user to manipulate rows and columns with the mouse but I was wondering if would it be possible. Thanks!

avatar image
1

Answer by tormentoarmagedoom · Mar 12, 2018 at 04:23 PM

I think the best way is to create EmptyObjects as pivots, and then make parent and unparent each cube to the correct pivot for each movement.

Comment
Add comment · Show 1 · 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 gemmaF · Mar 12, 2018 at 04:55 PM 0
Share

Thanks, this is helpful! It would be better to do that in the Inspector or via script assigning each pivots a different name to identify them?

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

75 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

Related Questions

Move everything within Collider around an specific angle 1 Answer

Rotate object around another object C# 2 Answers

rocket passing rings 2 Answers

Smoothly rotate object based on GetAxis 1 Answer

Top down camera always facing north - avoid residual rotation 1 Answer


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