Rotating transform more than 180 degrees
Hi, I was wondering why my transform won't keep rotating after 180 degrees. The rotation must be set and not something like Transform.Rotate due to the circumstances in my game.
Here is the code: using UnityEngine; using System.Collections;
public class Test : MonoBehaviour {
public float speed = 10;
public float time = 0.25f;
void Start () {
StartCoroutine("Wait");
}
void Rotate () {
Vector3 rot = transform.rotation.eulerAngles;
rot += new Vector3(speed, 0, 0);
transform.rotation = Quaternion.Euler(rot);
StartCoroutine("Wait");
}
IEnumerator Wait (){
yield return new WaitForSeconds (time);
Rotate ();
}
}
Here is the result: https://gyazo.com/bd66665ac525b74afab5879c1a5d1a1a
You can rotate objects using quaternions ins$$anonymous$$d of euler angles. To rotate an object you just need to multiply it's rotation (which is a quaternion) by a quaternion representing rotation.
Something like that:
transform.rotation = transform.rotation * Quaternion.AngleAxis(5f, Vector3.up);
Answer by unity_bTkJGG31azdikQ · Jan 23, 2020 at 06:51 PM
I know this is four years old, but for posterity.
Converting from euler angle to quaternion and back won't always get you what you started with, since there's more than one way to represent a rotation with euler angles (well, with quaternions too). When I tested this code, it actually wouldn't rotate more than one step past 90 degrees. After setting rot
to (100, 0, 0)
, storing it as a quaternion and pulling it back into an euler representation, it turns out to be (80, 180, 180)
, and when you add 10 degrees to the x component of that, you move backward. Unity stores the quaternion representation and just gives the euler representation when it's asked for. In general, euler angles should only be used for humans to input and read rotations/orientations.
I don't understand why Transform.Rotate won't work for you, but if you must avoid it, you might try storing the euler representation as a class variable to ensure it stays as expected. The downside here is that if you're rotating around multiple axes, you might also get unexpected results because euler rotations mean rotations around specific axes in a particular order. If you're just doing one axis for a given object, you should be fine, though.
Answer by DSivtsov · Aug 24, 2021 at 10:05 PM
Work Variant will be
void Rotate()
{
Quaternion deltaQuaternionRot = Quaternion.AngleAxis(speed, Vector3.right);
transform.rotation = transform.rotation * deltaQuaternionRot;
StartCoroutine("Wait");
}
I'm no big specialist in Rotation in Unity, but:
Rotation in Unity based on Quaternion mathematics where "rotation" = "Quaternion * Quaternion";
the
rot += new Vector3(speed, 0, 0);
is wrong because you simply summarize two Vectors (Vector3), thatrot = transform.rotation.eulerAngles
doesn't matter, because Vector3 is just a container for storing three unrelated numbers
P.S> Quaternion.Euler(new Vector3(100, 0, 0)) == Quaternion.Euler(new Vector3(80,180, 180))
but (Quaternion.Euler(new Vector3(100, 0, 0))).eulerAngles == new Vector3(80,180, 180)
. This is the specificity of the Quaternion to Euler conversion algorithm. ( not only Unity). I saw recommendation "Be careful with Get "Quaternion.eulerAngles()"
Your answer
Follow this Question
Related Questions
Why does assigning a quaternion to transform rotation work but calling transform.rotation. doesnt? 1 Answer
Euler Angles in code / inspector different (both local & global) 1 Answer
What is the difference between setting transform.eulerAngles and transform.rotation? 1 Answer
transform rotations overwriting eachother 1 Answer
I cant find the issue on my code (Quaternion.Slerp) 1 Answer