Rubics cube

Posted by admin on May 30, 2011 in Unity3d |

A Rubik’s cube scripting example

I have recently came to an idea of a Rubic’s cube as a nice scripting example.

Rubik® and Rubik’s Cube® are registered trademarks throughout the world of Seven Towns Limited. Seven Towns Limited is the exclusive licensee of copyright in the Rubik’s Cube puzzle. This post regards to Unity3d cube mechanism scripting example, not the game itself.
My idea of scripting the cube movement is based on selecting the relevant rows of cubes, placing them in rotation container, rotating container and putting it back from the container to the cube.

In my example I made use of the fact that the same script can run cubes of different sizes and you can also set it here to any integer starting from 2.

People cope with with Rubic’s with at various levels of ease and the script lets them to train on small sizes to slowly work out the algoritmh and then move to bigger sizes.

After setting the size you need to reset the cube in order to apply the new size.

You can adjust the view of the cube by dragging the mouse over the application window and scrolling the mouse in order to zoom in/out.


rubik's cube

blank colouredarrow texturedfully textured

2x2x2 cube3x3x3 cube4x4x4 cube


All the meshes are created dynamically and uv-mapped, and there is a possibility to add to the difficulty level by texturing the individual tiles or the whole sides of the cube. When the tiles are individually mappped (with arrows in this case), then – for the inside tiles – not only the color but also the rotation matters. When the sides are mappped with picture, then – for the inside tiles – not only the color and rotation, but also the strict position matters.

The settings and textures can be customized by image and xml data held in separate data folder.

In the web version there are also two php scripts for communication with mysql databases for reading and storing the scores.

In the standalone versions the scores are stored on the local computer in PlayerPrefs.

The above download is obviously targeted at Unity3d enthusiast interested in this game setup. In order to try to play the game just click the picture on the top so that opens the link to the WebPlayer or download any of the standalone versions below:




The deployments contains a customizable xml file pointing to the texture. By the modification of this you can easily put your own logo or custom graphics on the cube.

Unity3d logo textureLinux logo textureChrome logo texture

I have also utilised an interesting Unity3d ability to pick a localization  based on the user’s system language with the Application.systemLanguage method. The default GUI language is English, but on the detection of Polish, the application reads that from the attached XML file.

The script could of course be also applied to other geometric regular and non-regular forms like pyramids.

This is a cube mechanism script. A far more challenging would be to create a general solving algoritmh for n-cube, that would be able to find the optimal solution in minimal number of steps. I have found some considerations on that subject here:

and there:


  • Frans says:

    I tried to modify your project, but I get the problem, in the meshes I do not find the right design, you can share files blender or its untity complete ..? thank you

  • Frans says:

    mmmm… i already try that but doesn’t working, for colour it’s okay but for number i don’t think it is working…?? i try modify your cube to be sudokube (numeric cube).. i’ve working many times but it doesn’t working… any advice…??

    • admin says:

      You are right, I have just opened that and made some settings in the inspector, because it didn’t work they way I intended. And I have reuploaded again, so check again and let me know. Basicaly it should work straightaway once you open the scene and enter the play mode.

      • Frans says:

        really …?? are you reaupload again..?? yap.. i’m still try modified your project.. that’s why i’m asking “do you have blender file” because it’s dificult too find the right meshes… i’m still try to modified your project.. your project helping me a lot.. thank you… 😀

        • admin says:

          Yes the upload is updated now. So when you start the playmode, the default playable 3x3x3 cube should appear. But don’t look for any meshes there, because there aren’t any because the mesh is procedurally generated so can only appear at runtime or playmode.

          • Nettie says:

            Chào Phú, sau khi học xog 2 năm tại FPT Poeitlchnyc, em có thể liên thông những trường Đại học có cùng ngành và nhận tuyển liên thông em nhé.

  • Frans says:

    yap… i know already about meshes it was procedurally generated so can only appear at runtime or playmode…. mmm are you sure about reupload the new file..?? the file, it’s only 1.2 MB but when i open the file it’s only 0 KB…. file download “Download Unity3d package (40387) |uploaded 2012-12-16” is it right.?? i’m sorry if i made a mistake..

    • admin says:

      Yes, I now am sure, because I’ve tested that.
      I have Unity 4.2 Pro but this should work the same in the free version. I’ve created a new project, then imported this as the custom package, opened the included scene, entered the playmode and the 3x3x3 cube has appeared!. No, wait a minute, still there is something wrong with it.

  • Frans says:

    I’m sorry if I made a mistake.. link download is active.. but when the file is downloaded, file is corrupted… i try download it bunch of times… but the it’s still same i can’t open it… my unity version is 4.1.2 but thinks ther is no problem about that…

  • Frans says:

    I suggested to try another server.. like google drive, dropbox or mediafire to reupload the file… really i can download it… file is corrupted.. thanks before.. 🙂

  • Frans says:

    I think there is no difference between the first and the new one…? maybe main camera always move when mouse is moved…
    I wanna ask you something:
    1. if you wanna put some number 1 to 9 in some cube, what vector you want to change to put that number.?
    2. for algorithm, what do you suggested as solver..?? I’ve been try genetic algoritm and that’s quite good as solver.

    • admin says:

      1. There is a tree texture there – replace that with your own numbers texture and you will have numbers.
      2. There is no solver there, just a function that checks if the cube is solved after each move, but I know there are cube solving algorithms and not much more – to be frank.

  • μεταξύ says:

    Can I just say what a comfort to find an individual who
    truly knows what they’re talking about over the internet. You definitely
    know how to bring an issue to light and make it important.
    More and more people should look at this and understand this side of your story.
    I was surprised you are not more popular since you surely have the gift.

  • Kim says:

    How can I change the mouse control to swipe control sir?

    Thank you.

  • 95Shirleen says:

    Hello blogger, i must say you have very interesting posts here.
    Your website should go viral. You need initial traffic only.
    How to get it? Search for; Mertiso’s tips go viral

  • AFG says:

    I’m seeing Purple Triangles in Unity3d 2017 – maybe a mesh disappeared?

    Is it possible to amend the License on the scripts? I am willing to but the license for commercial purposes. Also, JavaScript will soon be obsolete in Unity – do you mind me making a C# version? A-lot of the features are broken in the newer engines. Would I be under the same License if I fixed and Ported this?
    Thank in advance.

  • AFG says:

    Sorry I replied to myself:
    To be clear, do we have the equivalent to an MIT – License with regards to the Rubiks Cube code?

    • admin says:

      I am not sure what you mean.
      But I have assumed at the time at publishing it that Rubiks Cube is one thing – and I lave put the MIT licence hare – and the Rubiks Cube game idea itself is the second thing, which I have not sorted out – in fact. But I have seen a number of Rubiks cube apps on the web, and haven’t seen any licensing references there.

Leave a Reply

Copyright © 2011-2024 virtualPlayground All rights reserved.
VP based on Desk Mess Mirrored v1.8.3 theme from