Vector, aligned to surface

Hello. I have a rather simple, vector-related issue. It’s a really simple thing which I can’t get to work.

I have a ray which comes from an object named “A”. The ray hits a surface called “B”. How can I get a vector that is aligned to the surface and points at A.forward’s direction. This might be confusing, so I drew a little scheme:

So in this scheme, Vector 1 would be the ray that hits the surface. As you can see, the objects is facing forward (so Desired vector DOT object.forward is bigger than 0) but is also facing a little upwards. However, the desired vector replicates the object’s forward but it’s not facing upwards, instead it’s mapped to the surface.

So how can I do this ? I tried crossing vectors, subtracting and normalizing…pretty much nothing that came to mind worked.

EDIT: just had an idea, but still can’t get it to work properly. Here’s some code:

var rotateVector : Quaternion = Quaternion.AngleAxis(90-Vector3.Angle(object.forward, surfaceNormal), object.right);
var desiredVector: Vector3 = rotate the object.forward vector with rotateVector…somehow.

If I manage to rotate the vector, would that work ?

Yes, I did it. Looking at the scheme I drew for the thread I actually came up with the idea for the solution. It’s really a simple one:

var rotateVector : Quaternion = Quaternion.AngleAxis(90-Vector3.Angle(object.forward, surfaceNormal), object.right);
var tdir : Vector3 = rotateVector * object.forward;

And this is it. This will generate the desired vector. I helped myself here, hope this thread helps somebody else :slight_smile:

1 Like

Too complicated.

I used this:
float Side = Vector3.Cross(transform.forward, hit.normal).y < 0 ? 1 : -1;
Vector3 DesiredVector = Vector3.Cross(transform.up, hit.normal) * Side;

However I am looking for a much simpler way…