Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by Oorlfe · Jul 27, 2015 at 10:54 AM · rotationinputarrayforeachrubikscube

Rubik's Cube, grouping and rotating problems

Hi,

I'm making a Rubik's Cube type of situation with 3x3x3 cubes. I've made some progress, but I've run into several questions. At this point I have been able to successfully gather nine cubes and rotate them smoothly 90 degrees, but only if the function that gathers them is called from the Start function. When I try to call it from an input its jumpy and spins farther than 90 degrees. When testing my code on a single cube not requiring the foreach loop it works fine. In the course of trying to figure it out I have come across several things I can't explain that may be a part of the issue. Either way I'd love to know why those things are happening as well.

The first, when my "endRotation" and "rightPivot" variables are instantiated Unity makes 27 of them, one for every cube, rather than just the 1 I need. Second, when I have tried to apply a counting variable, "i", to the foreach loop I have found that it only goes up to 9 and then starts over for a total of 243 times. Also, when I have tried to apply a second counting variable, to count every time "i" gets to 9, it never gets higher than 1.

Thanks in advance for any help.

public class ComboRotator : MonoBehaviour { private GameObject[] cubes; private GameObject rightPivot;

 private GameObject endRotation;

 private bool gathered;
 
 void Start () 
 {
     rotateGo = false;
     gathered = false;

     endRotation = new GameObject ("endRotation");

     cubes = GameObject.FindGameObjectsWithTag ("Cube");
 }

 void Update () 
 {
     if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         if (gathered == false)
         {
             GatherRight ();
         }
     
         endRotation.transform.Rotate (Vector3.right, 90, Space.World);
     }
     
     rightPivot.transform.rotation = Quaternion.Lerp (rightPivot.transform.rotation, endRotation.transform.rotation, Time.deltaTime * 8);
     }
 }
 
 void GatherRight()
 {
     rightPivot = new GameObject ("rightPivot");  
     rightPivot.transform.position = new Vector3 (2, 1, 1); 

     foreach (GameObject cube in cubes) 
     {
         if (cube.transform.position.x == 2) 
         {
             cube.transform.parent = rightPivot.transform;
         }
     }

     gathered = true;
 }

}

Comment
Add comment · Show 3
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 Oorlfe · Jul 27, 2015 at 06:13 PM 0
Share

I found another oddity. I tried to assign the cubes to "rightPivot" directly, one at a time without the foreach loop and discovered that order in the array does not correspond to the order in the Hierarchy. Calling cube[0] gives cube[24], calling cube[1] gives cube[8], etc. I don't see any obvious pattern to it. As with the other questions I'm finding I'm not sure if it is effecting my main issue or not, but would love an explanation as to why this is happening.

Thanks

avatar image NoseKills · Jul 27, 2015 at 08:00 PM 1
Share

The "second oddity" might be explained by this ?

The code you are showing clearly shows you create just one "endRotation" GameObject, so if you are getting 27 of them, this script is clearly attached to more than 1 GameObject and that's why everything happens multiple times. Do you perhaps attach this script to 1 Cube in the scene and the clone that cube to make the other 26 ? The code you show here doesn't give any clue about how you create the whole setup.

27 = 3*9 and 243 = 9*27 so there's clearly a pattern that indicates some unintentional recursion.

avatar image Oorlfe · Jul 28, 2015 at 04:37 AM 0
Share

Nose$$anonymous$$ills!

Thanks! You are right I had my script attached to every cube. I left it attached to just 1 and it works. The rotation stops at 90 degrees and I only get 1 instance of "endRotation" and "rightPivot". Also, because of that fix the looping/counting variable issue is no longer applicable. I'm not sure why this was having the particular affect that it was, but it is resolved.

The only thing left is that my cubes[] array is still taking the cubes out of order. I would love to know why that is the case, but for now it doesn't seem to be having an adverse effect.

Thank you again!

2 Replies

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

Answer by DiegoSLTS · Aug 02, 2015 at 04:41 PM

This:

 if (cube.transform.position.x == 2)

is a bad idea. transform.position.x is a float, and comparing floats for equality (==) is usually the source of bugs, your compiler might be showing you a warning about it too.

Like I said in your other question, float operations produce some errors inherent to float (or double) variables. When comparing against a float use <, , >= (even != is a bad idea). If you want cubes with x position "equals" to 2, write something like:

 if (Mathf.Abs(cube.transform.position.x - 2f) <= Mathf.Epsilon) {
     //something
 }
 

Epsilon is: http://docs.unity3d.com/ScriptReference/Mathf.Epsilon.html

Anyway, you might want to do something different, maybe use int variables to keep track of where a cube is in the rubik cube, like coordinates (face, row and column), and then you can check for equality (all cubes with the same "face" value form a face). You just update the values whenever you do a rotation.

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 Oorlfe · Aug 03, 2015 at 09:44 PM 0
Share

Thanks DiegoSLTS!

Very helpful. $$anonymous$$y cube is working great now.

avatar image
0

Answer by thelasthope313313 · Jun 15, 2017 at 11:55 PM

hi i cant access to unity doc cause of sanction what should i do can sb send the docs to my mail?

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need help on the 3ds max style camera control 0 Answers

Gyro calibration in android? 0 Answers

Moving an object in a circle towards joystick 0 Answers

how to input words from array to GUI? 0 Answers

Rotate Vector3 array around a point 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