Here's a way to get the local rotation of just the yEDIT: The below quickly breaks down when more than one euler axis is non-axiszero. This function can be modifiedRefer to get the x or z-axisComponent of a quaternion rotation around an axis instead.
/// <summary> isolate the y-Component of a rotation </summary>
private Quaternion yRotation(Quaternion q)
{
float theta = Mathf.Atan2(q.y, q.w);
// quaternion representing rotation about the y axis
return new Quaternion(0, Mathf.Sin(theta), 0, Mathf.Cos(theta));
}
You can verify the result in(thank you @LyrePyre for noticing and @minorlogic for the Unity inspector by converting to Eulerlink).
Original:
public float yLocal;
void Update()
{
yLocal = yRotation(this.transform.rotation).eulerAngles.y;
}
Here's a way to get the local rotation of just the y-axis. This function can be modified to get the x or z-axis.
/// <summary> isolate the y-Component of a rotation </summary>
private Quaternion yRotation(Quaternion q)
{
float theta = Mathf.Atan2(q.y, q.w);
// quaternion representing rotation about the y axis
return new Quaternion(0, Mathf.Sin(theta), 0, Mathf.Cos(theta));
}
You can verify the result in the Unity inspector by converting to Euler:
public float yLocal;
void Update()
{
yLocal = yRotation(this.transform.rotation).eulerAngles.y;
}