Weird results using Quaternion.Euler(x, y, z) * transform.forward

Hello. I’m trying to make a raycast from a position to a direction based on transform.forward.
Here is what i’m doing :

Vector3 rotation = m_Rigidbody.transform.forward;
rotation = Quaternion.Euler (0, 45, 0) * rotation;
Debug.DrawRay (m_Rigidbody.transform.position + m_Rigidbody.centerOfMass, rotation, Color.red, 0.5f, false);

Basically i’m multiplying the forward vector by a quaternion.

With Quaternion.Euler(0, 45, 0) i obtain this : 0, 45, 0 - YouTube
It’s the expected result. As you can see, the red ray is at 45 degrees around the y axis, based on the forward vector.

With Quaternion.Euler(45, 0, 0), i expect it to be oriented by 45 degrees around the x axis, to make it point to the ground or to the air. But, this is the result : 45, 0, 0 - YouTube

It’s not constant, why ?

What is the problem ?

Thanks you !

Your quaternion is a world space rotation. So you rotate your vector in worlspace around the worldspace x axis. Of yource since you never rotate around your z axis the orientation of the y axis is the same between local and worldspace. However your x axis is different.

Basically you have two options:

  • Use the same quaternion but rotate the local space forward vector and then transform it into worldspace m_Rigidbody.transform.TransformDirection(Quaternion.Euler (45, 0, 0) * Vector3.forward)
  • The other way is to use a Quaternion around the x axis of your object: Quaternion.AngleAxis(45, m_Rigidbody.transform.right) * rotation

I think the second solution would be the most readable.