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 /
avatar image
0
Question by cipherr · Sep 13, 2014 at 08:43 PM · rotatecubetrack

Tracking the rotation of a cube

Hello, I'm at my wits end here. What I'm trying to do seems simple in theory, but Unity doesn't seem to want to behave. What I'm doing is generating a random number between 1 and 5. Each of those numbers corresponds to the direction a cube should rotate (1 = 90 degrees up, 2 = 90 degrees down, etc (and 5 = 180 rotate on y axis)). This is all in a for loop. However, weird stuff starts happening, and I don't know why. On the first command, the cube rarely rotates right (usually up to three times to intended value), and then there are problems where if the Y axis is 90, 270, etc, you need to rotate on the Z axis instead of the X. And then sometimes it works fine!

Here's a watered down snippit of the code. I removed the stuff that deals with that Z axis thing, since it's just a few if statements for the specific cases.

 IEnumerator timeSpeed(){
         for(int i = 0; i < totalCommands; i++){
 
 
             nextDirection = Random.Range(1, 6);
 
             if(nextDirection == 1)
                 trackX -= 90;
             if(nextDirection == 2)
                 trackX += 90;
             if(nextDirection == 3)
                 trackY -= 90;
             if(nextDirection == 4)
                 trackY += 90;
             if(nextDirection == 5)
                 trackY += 180;
 
             yield return new WaitForSeconds(time);
 
             if(i == totalCommands-1){
                 gameOver = true;
                 StopCoroutine("timeSpeed");
             }
         }
     }

Does anybody know another way to do this? Or a solution to my issue? It would be so helpful.

Thanks!

Comment
Add comment · Show 5
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 cipherr · Sep 15, 2014 at 10:46 PM 0
Share

I actually did post the code that deals with the rotation. Although I did fail to mention that trackX and trackY are the variables in place for the x and y rotation of the object. One of the main problems I'm still having is that sometimes Unity rotates the object 3 times ins$$anonymous$$d of 1.

Ex:

 if(nextDirection == 1){
    trackX -= 90;
    print("hit");
 }


sometimes this (or any of them) will trigger. The print statement will only get called once, and the rotation will go from (0,0,0) to (-270,0,0)

avatar image killa247 · Sep 15, 2014 at 11:12 PM 0
Share

wouldn't class this as an answer but maybe if the rotation is 0,0,0 the cube is getting a $$anonymous$$us value, so 0 - 90 = -270 because 0 is also -360. make the random.range even out to the nearest int. Put a Debug.Log("This is the Error: " + nextDirection); under the random part, and maybe with the if statements do this

 if (<Code>) 
 {
 
 }
 
 Else if (<Code>)
 {
 
 }
 
 Etc...
 
 //the last one in the sequence should just be a else () {}
 

avatar image rutter · Sep 15, 2014 at 11:14 PM 0
Share

How are trackX and trackY actually being used? You didn't post any code that modifies or assigns a rotation.

avatar image glenwatkinson · Sep 16, 2014 at 12:06 AM 0
Share

$$anonymous$$aybe you should double-check whether you should be using Space.world or Space.self.

avatar image cipherr · Sep 17, 2014 at 02:57 AM 0
Share

Sorry, for some reason I was dumb and didn't think how I assigned the cube to be rotated mattered. It totally does. I was using Euler angles, which was a big part of the issue, but there was a lot more to it than that. I posted the correct version below for those who care! Thanks a lot of the help

1 Reply

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

Answer by mouurusai · Sep 16, 2014 at 12:48 AM

Just do not use Euler angles.

 using UnityEngine;
 using System.Collections;
 
 public class RotationTest : MonoBehaviour
 {
     public float rotationSpeed = 180;
     public float siclePauseTime = 0.5f;
 
     void Start ()
     {
         StartCoroutine(RotationInitializer());
     }
 
     IEnumerator RotationInitializer()
     {
         while (true)
         {
             int axis = Random.Range(0, 3);
 
             switch (axis)
             {
                 case 0:
                     yield return StartCoroutine(RotateTo(transform.rotation*Quaternion.AngleAxis(90 * Mathf.Sign(Random.Range(-1, 2)), Vector3.up)));
                     break;
                 case 1:
                     yield return StartCoroutine(RotateTo(transform.rotation*Quaternion.AngleAxis(90 * Mathf.Sign(Random.Range(-1, 2)), Vector3.right)));
                     break;
                 case 2:
                     yield return StartCoroutine(RotateTo(transform.rotation*Quaternion.AngleAxis(90 * Mathf.Sign(Random.Range(-1, 2)), Vector3.forward)));
                     break;
             }
 
             yield return new WaitForSeconds(siclePauseTime);
         }
     }
 
     IEnumerator RotateTo(Quaternion target)
     {
         while (Quaternion.Angle(transform.rotation, target)>0.5f)
         {
             transform.rotation = Quaternion.RotateTowards(transform.rotation, target, rotationSpeed*Time.deltaTime);
             yield return null;
         }
         transform.rotation = target;
     }
 }

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 cipherr · Sep 17, 2014 at 02:59 AM 0
Share

Yeah, I was using Euler angles, which messed it up a bit more than I would have liked. Your code came really close to what I wanted, I just made some $$anonymous$$or adjustments. Below is my version of the code that gets the behavior I want. Aka the cube will always rotate up/down/left/right, and not just rotate in place. Thanks for the help!

 using UnityEngine;
 using System.Collections;
 
 public class TestS : $$anonymous$$onoBehaviour
 {
     public float rotationSpeed = 180;
     public float siclePauseTime = 0.5f;
     
     void Start ()
     {
         StartCoroutine(RotationInitializer());
     }
     
     IEnumerator RotationInitializer()
     {
         while (true)
         {
             int axis = Random.Range(1, 5);
             
             switch (axis)
             {
             case 1:
                 print ("up");
                 yield return StartCoroutine(RotateTo(Quaternion.AngleAxis(90 * $$anonymous$$athf.Sign(Random.Range(-1, 2)), Vector3.up)*transform.rotation));
                 break;
             case 2:
                 print ("right");
                 yield return StartCoroutine(RotateTo(Quaternion.AngleAxis(90 * $$anonymous$$athf.Sign(Random.Range(-1, 2)), Vector3.right)*transform.rotation));
                 break;
             case 3:
                 print ("down");
                 yield return StartCoroutine(RotateTo(Quaternion.AngleAxis(90 * $$anonymous$$athf.Sign(Random.Range(-1, 2)), Vector3.down)*transform.rotation));
                 break;
             case 4:
                 print ("left");
                 yield return StartCoroutine(RotateTo(Quaternion.AngleAxis(90 * $$anonymous$$athf.Sign(Random.Range(-1, 2)), Vector3.left)*transform.rotation));
                 break;
             }
             
             
             yield return new WaitForSeconds(siclePauseTime);
         }
     }
     
     IEnumerator RotateTo(Quaternion target)
     {
         while (Quaternion.Angle(transform.rotation, target)>0.5f)
         {
             transform.rotation = Quaternion.RotateTowards(transform.rotation, target, rotationSpeed*Time.deltaTime*5);
             yield return null;
         }
         transform.rotation = target;
     }
 }

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

6 People are following this question.

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

Related Questions

Object rotation to exact degrees not working 3 Answers

reseting rotation of objects? 0 Answers

Cube won't rotate in roll-a-ball tutorial 2 Answers

Make a cube rotate in right or left direction randomly 3 Answers

Rotate object with touch 4 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