If someone has a better question title let me know. I am doing something different in my game, but that explains what i am struggeling with.

This is my code i used to try understand quaternion rotation better for this scenario:

 // verifies that 90° turn on x axis points down (flying downwards)// expected : (0, -1, 0)// actual : (0, -1, 0)var down = Quaternion.identity * Quaternion.Euler(90, 0, 0) * Vector3.forward;var rightRotation = Quaternion.Euler(0, 90, 0);// verifies that right rotation applied to forward direction results in a right direction// expected : (1, 0, 0)// actual : (1, 0, 0)var right = rightRotation * Vector3.forward;// verifies that when flying downwards and doing a right turn it should be pointing right// expected : (1, 0, 0)// actual : (0~, 0~, -1)var alsoRight = (Quaternion.LookRotation(down, Vector3.forward) * rightRotation) * down;

however the resulting vector is (0, 0, -1) instead of (1, 0, 0).

So i guess the question here how i do i rotate a quaternion around a relative axis, instead of the world x axis?

If we assume the initial zero rotation is from behind i would expect it to fly towards the right vector while showing its belly.

Where am i going wrong with this?

1

Best Answer


This script helped me understand how to do it and why my previous attempts did not work:

public class EulerRotation : MonoBehaviour{public bool x;public bool y;public bool z;void Update(){if (z)transform.rotation = Quaternion.AngleAxis(0.1f, transform.forward) * transform.rotation;if (y)transform.rotation = Quaternion.AngleAxis(0.1f, transform.up) * transform.rotation;if (x)transform.rotation = Quaternion.AngleAxis(0.1f, transform.right) * transform.rotation;}private void FixedUpdate(){Debug.DrawRay(transform.position, transform.forward * -5, Color.blue);Debug.DrawRay(transform.position, transform.right * -5, Color.red);Debug.DrawRay(transform.position, transform.up * -5, Color.green);}}

when doing transform.rotation * modifiedrotation the result was always wrong. When doing modifiedrotation * transform.rotation i did get the results i was expecting.

So using Quaternion.AngleAxis is the right way to turn an object relative to its current rotation, but the axis rotation has to be multiplied by the current rotation, instead of the other way around.