How do you find the upvector of a RAYCAST normal vector

So whenever you perform a raycast, the only vector you’re given is the normal vector.


But I want to find the upvector of that normal vector.

Now I can’t just do “ray.Instance.CFrame.UpVector”, because sometimes the upvector of the instance could be below or to the right of the ray’s normal vector.
So what I’m trying to find is that blue axis which I have circled.

There is probably a way to do this with only one Cross but this works

normal:Cross(Vector3.new(0,1,0)):Cross(normal)
2 Likes

It doesn’t work, but if it would this is what I would want it to look like

CFrame.lookAt(ray.Instance.Position,ray.Normal.Unit,ray.Normal.Unit:Cross(Vector3.new(0,1,0)):Cross(ray.Normal.Unit))

Can you show me a video of you using it if there was a chance that I would be using your equation incorrectly?

This is what I got when I used yours

workspace.P1.CFrame=CFrame.lookAt(workspace.P2.Position,workspace.P2.CFrame.LookVector,workspace.P2.CFrame.LookVector.Unit:Cross(Vector3.new(0,1,0)):Cross(workspace.P2.CFrame.LookVector.Unit)
)

You can calculate it with CFrames:

local function onRaycast(normal)
    local cframe = CFrame.lookAt(Vector3.new(), normal)

    return cframe.UpVector
end

didn’t want to make a copy answer

There are too many unknowns to calculate an upvector
You can assume it’s based on 0,1,0 like the other people are suggesting but in most cases that’s only useful as a cosmetic
It would depend what you need this upvector for specifically because there’s no single up vector for a normal, it depends on what you’re doing

That literally gives nothing new. It would only give the upvector of the cframe with no upvector defined.

1 Like

What do you mean? Are you expecting ray.Instance.CFrame.UpVector to be the same as CFrame.lookAt(Vector3.new(), ray.Normal).UpVector?

How can an up vector not be defined for a CFrame?

Are you trying to get the vector parallel to the face and facing up? So perpendicular to the normal vector?

Sorry i just got back home and didnt see your reply.

The problem here is seems is that you are assuming that the unit vector is already relative to the position the ray hit.

Correction

CFrame.lookAt(rayPosition, rayPosition + calculatedVector)