How can I find a Vector3 which is perpendicular to given Vector3?
Maybe rotate the Vector3 90 degrees or something, is there a vector3 function that can do that?
This image illustrates what I am trying to accomplish:

The thing is, there is an infinite amount of vectors perpendicular to any given vector in 3D space. You need a second vector not parallel to the first one to find a vector perpendicular to them both, i.e. their cross product, since this way a plane is defined, which may have only one perpendicular line.
In Unity, cross product is computed by the static method Vector3.Cross().
To answer your original question: how to compute a vector perpendicular to another?
Let's call your vector p and the vector we're looking for that is perpendicular to it q. Note that there are an infinite number of vectors q but we're just going to find one.
For perpendicular vectors the dot product is always 0 so we find the equation p · q = 0 which we can write as p.x * q.x + p.y * q.y + p.z * q.z = 0. Now we're going to assign arbitrary values to q.x and q.y in order to fix one solution, let's pick q.x = 1 and q.y = 1. Then we're left with p.x + p.y + p.z * q.z = 0. Solving for q.z gives us q.z = -(p.x + p.y) / p.z.
In conclusion: the vector q = (1, 1, -(p.x + p.y) / p.z) is perpendicular to p.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With