# 从两个矢量旋转/方向 [英] Rotation/Orientation from two vectors

### 问题描述

``` ``` X =向量（0.707，0.707，0）
Y =向量（-0.707，0.707，0）
``` ```

``` ```函数RotationMaster（VX：Vector3类型，VY：Vector3类型）{
VAR VZ = Vector3.Cross（VX，VY）;

VAR X = Mathf.Atan2（-vz.y，vz.z）* Mathf.Rad2Deg;
变种Y = Mathf.Atan2（vx.z，其中Vx.x）* Mathf.Rad2Deg;
变种Z = Mathf.Atan2（-vx.y，其中Vx.x）* Mathf.Rad2Deg;

的debug.log（X：+ X +; Y：+ Y +; Z：+ Z）;

transform.rotation = Quaternion.Euler（X，Y，Z）;
}
``` ```

``` ``` RotationMaster（Vector3类型（1,0,0），Vector3类型（0,0.707,0.707））;
RotationMaster（Vector3类型（1,0,0），Vector3类型（0，-1,0））;
``` ```

``` ``` RotationMaster（Vector3类型（-0.5，-0.5,0.707），Vector3类型（-0.707,0.707,0））;
``` ```

``` ``` Quaternion.SetLookRotation
``` ```

I'm working on a small Unity3D project and I need to import some data from another tool. That tool gives me the object orientation through two vectors, and I need to port that to Unity.

As an example, I have these two vectors;

``````x = Vector( 0.707, 0.707, 0)
y = Vector(-0.707, 0.707, 0)
``````

With only this information, I need to understand that my object needs to rotate 45 degrees on the Z axis. The two vectors represent the up and right vectors of my object, not a starting point nor a destination.

EDIT

Ok, now I'm able to do single-axis rotation! Yeah! Provided this function;

``````function RotationMaster (vx : Vector3, vy : Vector3) {
var vz = Vector3.Cross(vx, vy);

var x = Mathf.Atan2(-vz.y, vz.z) * Mathf.Rad2Deg;
var y = Mathf.Atan2( vx.z, vx.x) * Mathf.Rad2Deg;
var z = Mathf.Atan2(-vx.y, vx.x) * Mathf.Rad2Deg;

Debug.Log("x : " + x + "; y : " + y + "; z : " + z);

transform.rotation = Quaternion.Euler(x,y,z);
}
``````

I can now pass simple rotation like that:

``````RotationMaster(Vector3(1,0,0), Vector3(0,0.707,0.707));
RotationMaster(Vector3(1,0,0), Vector3(0,-1,0));
``````

And as long as I'm not playing with multiple axis, it will work. But as soon as I try a more complexe rotation, it fails... As an example;

``````RotationMaster(Vector3(-0.5,-0.5,0.707), Vector3(-0.707,0.707,0));
``````

This is wrong... :-(

Any clue?

``````Quaternion.SetLookRotation
``````

was (with few tweaks...) what I was looking for! Thanks to all for your help!