Unity3d perpendicular vector3

时间:2015-10-30 22:13:53

标签: unity3d

I am trying to get a perpendicular vector3 from a vector3. Does anyone know how I can do that? Maybe rotate the vector3 90 degrees or somthing, is there a vector3 function that can do that?

*edit enter image description here

*Edit

This is what I want, I want the bottom part of my object facing to my surface(or a single point) how can I achieve that? I tried Quaternion.LookRotation and Transform.Lookat but they both use the forward vector of my object. I know that I also can define the world up vector in those functions but the problem is that I don't have one.

enter image description here

4 个答案:

答案 0 :(得分:1)

回答你原来的问题:如何计算垂直于另一个的矢量?

让我们调用你的矢量p,我们正在寻找的矢量垂直于它q。请注意,有无数个向量q,但我们只是找到一个。

对于垂直向量,点积始终为0,因此我们找到方程p · q = 0,我们可以将其写为p.x * q.x + p.y * q.y + p.z * q.z = 0。现在我们要为q.xq.y分配任意值以修复一个解决方案,让我们选择q.x = 1q.y = 1。然后我们离开p.x + p.y + p.z * q.z = 0。求解q.z会给我们q.z = -(p.x + p.y) / p.z

总之:向量q = (1, 1, -(p.x + p.y) / p.z)p垂直。

答案 1 :(得分:0)

3D空间中的这个垂直向量不是唯一的。但是,给定另一个向量,您可以获得一个垂直于它们的新向量。

Vector3 v1;
Vector3 v2;
Vector3 v3 = Vector3.Cross(v1, v2);

答案 2 :(得分:0)

事实是,在3D空间中有无限量的矢量垂直于任何给定的矢量。你需要一个与第一个矢量不平行的第二个矢量来找到与它们垂直的矢量,即它们的cross product,因为这样定义了一个平面,它可能只有一条垂直线。

在Unity中,交叉乘积由静态方法Vector3.Cross()计算。

答案 3 :(得分:0)

这解决了它:

Vector3 dir = ground.position - obj.position;
obj.rotation = Quaternion.FromToRotation(obj.up, dir) * obj.rotation;