- Home /
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!
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)
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 () {}
How are trackX
and trackY
actually being used? You didn't post any code that modifies or assigns a rotation.
$$anonymous$$aybe you should double-check whether you should be using Space.world or Space.self.
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
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;
}
}
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

Follow this Question
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