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:
- The Postion of the entry
- 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?