Quaternion.FromToRotation() 3D direction on 2 axes.

Hello. I play around with procedural generating rooms.
I created an small room with two entries. On every entry i put two helper objects wich hold the positon and direction. On every entry i want to connect other rooms.
This helper object are for:

  1. The Postion of the entry
  2. Wich directon is the entry.

With this two points, i can calculate the offset of the next room. So that it fit exactly to the existing entry.
For this calculation i need to turn the the offset to the direction of the door.

The Calculation → get the offset from an not instanciated prefab (it is based on quaternion.identity) → Turn this offset in direction of the door.

With this code i get the direction of the door.

Vector3 visitDirection = helper1.position- helper2.position;
Quaternion visitRotation = Quaternion.FromToRotation(Vector3.left,visitDirection.normalized);

With this code do i turn the offset vector.

 Vector3 turnedDirection = visitRotation * modelOffset;

When the direction of the base element is just turned on one axis from zero. Example for understanding, like Euler(0.0,33.0,0.0) or Euler(0.0,0.0,-100.0). Then it works fine and FromToRotation give me the right direction and the calculation will succsess.

Now the issue:

If the orientation of the base element is turned on 2 axes, also when i turn it in inspector. Like(0.0,12.5,43.3) In this case the Function Quaternion.FromToRotation() calculate the wrong direction. The code below proves it.

        Vector3 turn_Y_Axe = new Vector3(-0.7f,0.0f,0.7f); // -> Euler(0.0f,45.0f,0.0f)

        Vector3 turn_Z_Axe = new Vector3(-0.7f,-0.7f,0.0f); // -> Euler(0.0f,0.0f,45.0f)

        Vector3 turn_YZ_Axe = new Vector3(-0.5f, -0.7f, 0.5f); // -> Euler(0.0f,45.0f,45.0f)



        Quaternion rotation_Y_Axe = Quaternion.FromToRotation(Vector3.left,turn_Y_Axe);

        Quaternion rotation_Z_Axe = Quaternion.FromToRotation(Vector3.left,turn_Z_Axe);

        Quaternion rotation_YZ_Axe = Quaternion.FromToRotation(Vector3.left,turn_YZ_Axe);



        Debug.Log("Y Axe: " + rotation_Y_Axe.eulerAngles.ToString());

        Debug.Log("Z Axe: " + rotation_Z_Axe.eulerAngles.ToString());

        Debug.Log("YZ Axe: " + rotation_YZ_Axe.eulerAngles.ToString());



/*      Output log ************************************************

        Y Axe: (0.0, 45.0, 0.0)

        UnityEngine.Debug:Log(Object)

        Z Axe: (0.0, 0.0, 45.0)

        UnityEngine.Debug:Log(Object)

        YZ Axe: (346.4, 31.1, 46.4)  **--> Here exepted (0.0, 45.0, 45.0)**

        UnityEngine.Debug:Log(Object)

       **********************************************************/

Maybe I am doing something wrong with the quaternion?

Well your issue is that even you use quaternons you still think in euler angles rotations. That’s not how quaternions work at all. A quaternion defines a single rotation axis and the angle around that axis. The rotation axis can be arbitrary and with that you can actually represent any relative rotation. So while euler angles represent 3 consecutive rotation around the 3 world / parent space aligned axis in a certain order, quaternions will perform a direct rotation from any orientation to any other orientation directly around a single rotation axis.

From your description it’s actually a bit difficult to understand what your goal is. A quaternion will always carry out the shortest great-circle rotation which might not be what you want if you think in x-y orientations and you want to favor a certain euler angle axis before another. Keep in mind that FromToRotation only cares about a single axis that should be aligned through the generated relative rotation, not a whole orientation (which in 3d requires at least two direction vectors)

It’s often much easier to work with coordinate spaces and translate between them by using gameobjects and their transform components to represent a certain orientation. That way you can simply use TransformPoint / TransformDirection and InverseTransformPoint / InverseTransformDirection to convert from local space to world space or vice versa.