- Home /
transform.Rotate vs. transform.rotation = when use which?
Dear coding bros, I'm an absolute Unity noob. Could you be so kind and explain when it's better to use one and when the other of these two approaches? And what difference does it make? transform.rotation vs. transform.Rotate Thank you all. =========================
Thank you all for your replies, but I think I need to elaborate on my question: WHY would anybody use a ROTATION instead of ROTATE....and in WHAT cases EXACTLY. WHEN is it better to use ROTATION, when we already have ROTATE. Because I know that a gameObject can be rotated by it's transform using both. Give some brief examples of both cases, please.
There's no answer. You can Google and find lots of places where a person used one, but you or someone else would have used the other. That's the way program$$anonymous$$g works.
Rotate is a wrapper function -- a shortcut for a certain way of using rotation. Every program$$anonymous$$g environment has more shortcuts than any one person would ever want, and everyone always picks a few favorites.
An answerable question might be to ask how to rewrite a specific Rotate/rotation bit of code, using the other version.
Answer by Maui-M · Feb 14, 2014 at 05:18 PM
Rotation will set the objects rotation to that specified rotation. (As robertbu said, this should only be used by advanced users)
Rotate will take the current objects rotation and change it by the specified amount of rotation(Space.Self). (Useful for turning objects such as spinning or turning a car)
Rotate can also be used to rotate around other objects(Space.World). (Useful for rotating camera around player or orbiting planets.)
But setting rotation directly is often the obvious way. To snap an object "face up" use transform.rotation=Quaternion.identity;
. To aim it right, use transform.rotation=Quaternion.Euler(0,90,0);
.
Answer by RedDevil · Feb 14, 2014 at 07:59 PM
Easy way of puting it is that transfrom.rotation is used to rotate at a specific angle an object,while transform.Rotate can cause the object to rotate continuously.And by the way there is one you missed transform.RotateAround that can make the object rotate around another object or an axis.
Answer by robertbu · Feb 14, 2014 at 05:16 PM
You are mixing apples and oranges here. Transform.rotation is a Quaternion...an non-intuitive 4D construct. Unless you have a firm understanding of Quaternions, you should not assign to it directly. You can use the Quaternion class to construct many rotations and assign them. There is a Transform.eulerAngles that can be assigned to that makes a better comparison.
The typical properties that make Transform.Rotate() special vs. other forms of rotation are:
By default rotates around the local axes. Most other rotations default to using world axes.
It is a delta rotation. It rotates an object by a certain number of degrees. Most other methods of rotating an object are by default are absolute rotations. That is an object is placed in a specific rotation.
Rotations can be complicated, and there are all sorts of ways of implementing various rotations.
Answer by Ujjwal_Rajput · Sep 28, 2020 at 01:56 PM
trasnform.rotation take Quaternion , i think you should ask for trasnform.eulerAngles vs transform.Rotate here i an example - i want to move my player as my camera is moving (using cinemachine freeLookCamera) for i take the value of X-Axis from the freeLookCamera component and put this value to the player
void PlayerRotation() {
float angleY = cinemachineFreeLook.m_XAxis.Value;
Vector3 angle = new Vector3(0, 1, 0) * angleY;
if (transform.eulerAngles.y != angleY) {
//this.transform.Rotate(angle);
transform.eulerAngles = angle;
}
}
i am calling this function in FixedUpdate() method when i use transform.Rotate player continuously rotating base on angle but when i use transform.eulerAngles it only rotate when the camera is rotated.