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
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.
答案 0 :(得分:1)
回答你原来的问题:如何计算垂直于另一个的矢量?
让我们调用你的矢量p
,我们正在寻找的矢量垂直于它q
。请注意,有无数个向量q
,但我们只是找到一个。
对于垂直向量,点积始终为0
,因此我们找到方程p · q = 0
,我们可以将其写为p.x * q.x + p.y * q.y + p.z * q.z = 0
。现在我们要为q.x
和q.y
分配任意值以修复一个解决方案,让我们选择q.x = 1
和q.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)
在Unity中,交叉乘积由静态方法Vector3.Cross()
计算。
答案 3 :(得分:0)
这解决了它:
Vector3 dir = ground.position - obj.position;
obj.rotation = Quaternion.FromToRotation(obj.up, dir) * obj.rotation;