How to rotate an object around the world axis but not on the local axis?

Hello Devs,

I have a game object for which I am not sure of it’s X or Z angle during run time. The game object is highly altered during
runtime and the angles across X,Y and Z would be changing. But when the user gives the Input, it’s angles are directly influenced only by
the Input given by the user. At any point, the user might give an input as a target angle and I would like the game object to rotate from it’s rotation to
the desired target angles. The INPUT is basically only for X and Z and never Y. But while performing this I would like
the angle on Y axis never to be affected. I would like to have the rotation on WORLD and not LOCAL.

Restrictions :

  1. Target Angle based upon the user INPUT.
  2. GLOBAL rotation
  3. Angle across Y axis should not be affected.

I have hitting across this for a long time, taking breaks in between. Can someone help me out to sort this?

THANK YOU.

regards,

Karsnen

Hints: the Transform class offers two different variables: rotation and localRotation. The first one is the world rotation, the second is the local rotation.


edit: I understand your mistake. Do you know that a Quaternion has 4 elements and that those elements have nothing to do with the xyz axis?

If you want to retrieve the angle around x, y or z-axis, use transform.eulerAngles. But I would recommend to never deal with euler angles. Instead, you can deal with direction.

If you want to just use quaternions, you can do this:

Vector3 lSpeed = new Vector3(0f, 90f * Time.deltaTime, 0f);
Quaternion lCurrent = gameObject.Transform.rotation;
Quaternion lNew = lCurrent * Quaternion.Euler(Quaternion.Inverse(lCurrent) * lSpeed);

In the last line, it counteracts the current rotation. So, when we add the speed to the current rotation, it results in a rotation around the world axes.