- Home /
Difference between transform.localRotation = Quaternion and transform.RotateAroundLocal
I know there are other questions about the difference between these two things. But I haven't found anything regarding time.
As we all should know, if you call transform.Rotate and give an angle without scaling it using Time.deltaTime, the object rotates at insane speeds. I wanted to be able to clamp the angle of the rotation between 180 and -180. So I decided to add/subtract the rotationRate from a "currentAngle", and then assign that rotation using a Quaternion. To my surprise, I did not need to scale the rotationRate with time to get my desired result. Anyone have any insight to this?
public class RotateCannon : MonoBehaviour
{
public float rotationRate = 30f;
float currentAngle = 0f;
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
currentAngle -= rotationRate;
else if (Input.GetKey(KeyCode.RightArrow))
currentAngle += rotationRate;
transform.localRotation = Quaternion.Euler(0, 0, currentAngle);
}
}
Well that code looks like it would spin the object more than 5 times a second at 60hz - are you sure it isn't spinning so fast you are getting a strobe effect that makes it look like it is working? If you are clamping an angle somewhere that code isn't shown, perhaps something else if affecting it too?
The only thing I'm not including is the obligatory using statements. But there is no strobe effect. it is rotating properly without any artifacts or issues. If i switch to something like "translate.RotateAroundLocal" then it does spin so fast it produces a strobing effect. But not through the use of the quaternion. That's why I'm here asking what the difference is.
Well I use Quaternions for all my rotations and if I added 1800 to the rotation around the Z axis every second it would be spinning like a mad thing :)
Hey I can't post an answer either :(
I have just realised that RotateAroundLocal uses radians and of course Quaternion.Euler uses degrees making quite a big difference in rotation rate :)
That would be strange, since all Unity functions dealing with Euler angles use degrees, never radians. Only the $$anonymous$$athf functions use radians. But maybe that's why RotateAroundLocal is not (or no longer?) documented.
Answer by Wolfram · Jun 01, 2012 at 08:51 PM
Hmpf, there is a problem with the site or this question, I can't post an answer (also, my browser deleted my first answer before posting, for the same reason). So here it is as a comment:
Your code cannot produce a "properly" rotating object, unless you modified rotationRate in the Inspector, or there really is a strobing effect (use a different rate to check for that, such as 25).
The difference to RotateAroundLocal is that your code sets the angle absolutely, overwriting/ignoring previous values, while RotateAroundLocal adds the given angle to the transform's rotation each time you call the function.
Converted your comment to an answer - this worked, but UA really has something wrong with this question.
I think the other part of this answer is that point about radians and degrees though - that definitely accounts for a speed difference :)