Transform.Rotate issue

I can’t for the life of me figure out this problem! I have a sphere, and I want to rotate the sphere in the direction it’s going to simulate rolling. To do this I use this code:

transform.Rotate(180.0f * Time.deltaTime, 0.0f, 0.0f);

The problem is that when it rotates 180 degrees on the x axis it rotates 180 degrees on the y axis and z axis. This wouldn’t be a problem but in order to move the sphere forward I use this code:

transform.position.x += Mathf.Sin(transform.eulerAngles.y * (Mathf.PI / 180.0f)) * movementSpeed * Time.deltaTime;
transform.position.z += Mathf.Cos(transform.eulerAngles.y * (Mathf.PI / 180.0f)) * movementSpeed * Time.deltaTime;

Because the y angle rotates by 180 degrees the movement reverses direction. So why is transform.Rotate causing a 180 degree rotation on the y axis if it’s only rotating on the x axis.

Move the sphere forward by using transform.Translate(Vector3.forward… In order to do the rolling, have the sphere be a child of an empty game object, and use transform.Translate on the empty, and transform.Rotate on the sphere child. That way direction isn’t affected by the rotation of the sphere.

–Eric

Smart thinking! While the problem is essentially solved, I’m still curious why the 180 degrees is added. Do you have any ideas?

Not sure offhand; maybe related to the fact that Unity uses quaternions for rotation, and reading a single component of transform.eulerAngles can be “wrong” in isolation. (Not technically wrong, but a quaternion can be validly represented by euler angles in more than one way, so the results might not be what you expect. e.g. eulerAngles.y might be 180 or 0 depending on the rotation, so it’s best to read eulerAngles as a whole, and not just one component.)

–Eric

Maybe this will clarify it a bit for you. hold your left hand in front of you while making the axis with your fingers. Index finger point straight forward(z axis), thumb straight up(y axis), and middle finger pointed straight right(x axis). Now roll your hand along the middle finger until you’ve rotated it 180 degress… hint if your hand was pointing forward with thumb up and middle finger right when starting, swivel(read swing down then back) your arm to point backwards with index pointing straight back thumb straight down.

Now if you look… by rolling on the x axis, you’ve essentially rotated the other axis 180 degress… so up became down, forward became backwards, and of course x stayed the same since thats the one we were rolling on.

The easiest solution to your problem was pointed out, there are of course other solutions to this such as maintaining a direction vector. Then the local axis of your object wouldn’t matter since you’d use the direction vector to move the object. I’m probably not the best person to explain that, but I’m sure someone around here who has a good handle on 3d math could explain it if they were asked.

Thanks for the help! Aoon, your description made perfect sense. I think I should brush up on my quaternions =(