Bébul
Bébul

Reputation: 585

Edit or not the Transform.rotation in unity3D

In unity documentation, under Transform.rotation, there is written:

Do not attempt to edit/modify rotation.

But few lines after, in the example, the assignment is used transform.rotation = ... And setting transform.rotation directly seems to be everyday practice.

But can it be recommended? What about other properties, like localRotation, is the setter of the rotation property implemented so that the localRotation is changed too?

Upvotes: 1

Views: 4207

Answers (3)

Remy
Remy

Reputation: 5173

I feel like the wording on this in the docs used to be more clear, but this is what it implies:

It is highly discouraged to edit the transform.rotation of an object directly. that is to say doing something like transform.rotation = new Quaternion(0, 100, 15, 1); unless you really know the ins and outs of working with Quaternions, which are a great deal harder than working with EulerAngles which is more user friendly and easier to understand.

What you should be using instead (and this is also reflected in the code samples of the docs) are the methods made available by Unity to alter the rotation. These methods will deal with the complexity that comes with changing Quaternion values for you.

If we take a closer look at the code samples Unity provides:

 Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
 transform.rotation = Quaternion.Slerp(transform.rotation, target,  Time.deltaTime * smooth);

The two key factors here being target = Quaternion.Euler() which takes in three euler angles (angles on a 360 degree scale) and transforms them to Quaternions for you. And the other being rotation = Quaternion.Slerp() which takes in the current rotation as a quaternion, and a target rotation as quaternion and interpolates between the two.

Notice that neither of these two functions alters the transform.rotation "directly" from your side, but both pass through Unity's internal logic for converting to proper quaternions.

the same goes for methods like transform.Rotate.

transform.localRotation is basically the same as transform.rotation. The only difference being that the former indicates the rotation relative to the parent, and the latter its rotation relative to the world.

If you want to edit the rotation of an object directly it is easiest to edit the EulerAngles of the object. An example being:

transform.localEulerAngles = new Vector3(10, 150, 0);

Which will rotate your object 10 degrees around the X axis and 150 degrees along the Y axis, relative to its parent. Using transform.eulerAngles would rotate it relative to the world.

Note that, despite this being the easiest way to go around it. Unity does encourage using the Quaternions class and its functions to apply rotations, as using Euler angles can cause weird effects to happen.

There is also this doc talking about rotation and orientation in Unity which also states (emphasis mine):

Quaternions can be used to represent the orientation or rotation of a GameObject. This representation internally consists of four numbers (referenced in Unity as x, y, z & w) however these numbers don’t represent angles or axes and you never normally need to access them directly. Unless you are particularly interested in delving into the mathematics of Quaternions, you only really need to know that a Quaternion represents a rotation in 3D space and you never normally need to know or modify the x, y & z properties.

The reason for Unity using quaternions is, among others, that quaternions do not suffer from gimbal lock, which eulerAngles does. I quote from the article:

Unity stores all GameObject rotations internally as Quaternions, because the benefits outweigh the limitations.

Upvotes: 4

Laskio
Laskio

Reputation: 95

The components of transform.rotation are not angles and are not able to be handled as angles.

As robertbu mentions in this article:

Many rotations (and likely the one you were attempting) can be accomplished through other (simpler) methods, including 'Transform.Roate()' and directly accessing 'Transform.eulerAngles'-..

Transform.Rotate() - Rotate() does a relative rotation, and by default, a local rotation-..

Upvotes: 1

Loafwad
Loafwad

Reputation: 49

What they mean by not editing/modifying rotation is that rotation is a built in class by unity that handles rotations for objects. So feel free to set the rotation of an object like so:

Transform.rotation = object.transform.rotation;

But modifying the built in rotation class isn't recommended.

Upvotes: 0

Related Questions