0
\$\begingroup\$

Look at this image, I selected an object and pivot is set to local:

enter image description here

When I rotated selected object in the scene by X axis, it rotates exactly I wanted.

enter image description here

But if I rotate axis from inspector or script, it doesn't rotated to desired angle.

I'm not sure, but it seems it's related about Quaternion, but everything I tried doesn't worked well.

How do I rotate that object just like in rotated from scene?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

The (local) transform gizmo shows you rings of possible rotations from the current orientation, about the current local x, y, and z axes respectively.

The Inspector / transform.localEulerAngles property show you the current local orientation expressed as Tait-Bryan angles (commonly called Euler angles) with x, y, and z components.

These are not the same thing.

Tait-Bryan angles are not a point in rotation space. They're directions for how to get there. In Unity's formulation, they say:

  1. Starting from the default/identity orientation

  2. Rotate y degrees clockwise around your local y+ axis
    (note that this turns the local x & z axes)

  3. Next, rotate x degrees clockwise around your new local x+ axis
    (note that this turns the local y & z axes)

  4. Finally, rotate z degrees clockwise around your new local z+ axis

So the y rotation always happens first and z rotation always happens last. (If you think of the rotations as local-space. If you think of them in parent space, the order is reversed but the result is the same)

But using the transform gizmo, you can grab & turn the green & blue rings in any order you want, or even interleave them. Three numeric fields aren't enough to specify this kind of interleaving, at least not alone.

If you want to get the result of turning the green (y+) gizmo in local mode no matter what orientation you're starting from, in code you can write any of these equivalent expressions:

  • transform.Rotate(Vector3.up, yawAngle)
  • transform.Rotate(0, yawAngle, 0);
  • transform.Rotate(transform.up, yawAngle, Space.World);
  • transform.rotation = transform.rotation * Quaternion.AngleAxis(yawAngle, Vector3.up)
  • transform.rotation = transform.rotation * Quaternion.Euler(0, yawAngle, 0)

  • transform.rotation = Quaternion.AngleAxis(yawAngle, transform.up) * transform.rotation;

\$\endgroup\$
2
  • \$\begingroup\$ Thanks, now I can rotate exactly what I want, but still have one more problem. I need to set rotation exactly same as input from player, not adding rotation. For instance, if there is a slider and let's assume that I want to set rotation of that object from slider. Adding rotation doesn't work because if input range is -10 to 10 and input -10 and -5 rotates same direction because it just adding -5. What I need is set the rotation value directly from slider, not adding it, but I don't know how get initial rotation in this case. \$\endgroup\$
    – modernator
    Commented May 17, 2018 at 14:20
  • \$\begingroup\$ It sounds like you should ask a new question, showing your desired player interface and describing/diagramming/animating how you want the object to react to different combinations of input. \$\endgroup\$
    – DMGregory
    Commented May 17, 2018 at 14:35
1
\$\begingroup\$

It seems you are trying to tell apart the local from the world space. From the scene, you can press a button next to pivot, to tell apart the local from the world space.

When it comes to scripting, it all depends what code you are using. By using transform.localRotation you affect the object's local rotation (what you see in the inspector). By using transform.rotation you are affecting the object's world rotation.

You can also use the transform.Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self) function, which by default rotates around its local axis, but by changing the relativeTo variable, it can make a rotation in world space.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .