Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Rubic's cube problem select row/column

Discussion in 'Scripting' started by Bolt, Jun 6, 2018.

  1. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    Hi everyone, I'm developing a prototype of Rubik's cube. I have to select a row of cubes that I can run both vertically and horizontally. How can I do?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You could try something like this :
    1. Create an empty GameObject at the central position of the RubikCube (let's call this RC).
    2. Place the individual blocks in their appropriate locations around RC- but don't parent them to RC.
    3. Now, when you have selected a row (or rows) / column (or columns) for rotation, you can parent each block to RC, rotate RC accordingly and then unparent them afterwards.
    4. Of course, you will also want to update any internal data structure that you have that records which block is on which face of the cube.
     
  3. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    the problem is that when I click a block I have to understand which direction must rotate to associate certain blocks
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Yes that is the problem with a Rubik's cube implementation. :)

    What data structures have you considered so far to try and solve this?
     
  5. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    I thought to find the direction where I drag the mouse and based on the result I managed the movements
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    That certainly sounds reasonable. So the problems to approach are :
    1. Which block has been clicked on?
    2. Which direction was the mouse subsequently dragged (effectively up, down, left or right)?
    3. Knowing those 2 things, now need to be able to identify the blocks that are in the horizontal, or vertical, set of blocks to rotate.
    4. Then perform the animation (I presume you want to animate rather than have an instantaneous movement).
    5. Once the move is complete, you will want to update the data structure that indicates where the blocks are. Some of them will have changed the face they have ended up on.
    Maybe start to tackle each point in turn. For example, for point 1, can you place a single GameObject in a scene that is capable of detecting when it has been clicked on? How could you do that? Then move on to the next step, and so on.

    If you get stuck, there is always the "Learn" link at the very top of this page. That has lots of helpful tutorials to get you started. :)
     
  7. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    Thank you very much for the help I'll try to take one step at a time :)
     
    Doug_B likes this.