Find Vector3 perpendicular to Vector3 A in direction of Vector3 B

I haven’t been able to find much help on the matter of finding a perpendicular vector using two inputed vectors. Sure I’ve found help with just the perpendicular part, but not in a given direction.

I’m trying to calculate a new XYZ axis for movement on my game. The Y axis is given by the surface normal (lets say this is Vector A:(0,0.7,-0.7) a slope that goes up if moving forward). To be simple the forward direction (Vector B) will be (0,0,1.0), however this could be any combination depending onthe rotation of the player. I need to find the Vector perpendicular to the normal (Vector A) but in the direction of my players forward (Vector B).

How could I go about doing this?

You just need to project your vector B onto the plane with the normal vector A

All you actually need is the dot product, however your vector A need to be normalized. Unity provides a well optimised method which projects a vector onto a given normal, even when it’s not normalized: Vector3.Project().

All you need to do is subtract the projected vector B from the original vector B:

    // C# / UnityScript
    var vec = B - Vector3.Project(B,A);

If you want the new “vec” to have the same length as the old B you can do this afterwards of course:

    vec = vec.normalized * B.magnitude;

Since the question got already bumped i should add that Unity recently added a dedicated method for this kind of projection: Vector3.ProjectOnPlane.

The method literaly is implemented the same way as my solution above, so it’s just a bit simpler to use:

var vec = Vector3.ProjectOnPlane(B,A)