Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to get the cframe of the endpoint of a raycast?

so lets say you have a raycast

ray = ray.new(start,endpoint-start)

then you do the hit thing

local hit,pos,norm = game.Workspace:FindPartOnRay(ray)

now my problem is that when i do

print(pos)

it only gets the vector3 i need a CFrame because i need the direction that the ray is facing too is there any way to do this?

1 answer

✓ Accepted answer

If you're getting the position using FindPartOnRay then presumably you have the ray itself. To get the direction of the ray you can simply access its Direction property:

local ray = Ray.new(...)
local direction = ray.Direction

You could then use this to construct a CFrame if you needed to. One way would be to use CFrame.fromMatrix:

local ray = Ray.new(start, endpoint-start)
local hit, pos, norm = game.Workspace:FindPartOnRay(ray)
local direction = ray.Direction

local lookVector = direction
local rightVector = direction:Cross(Vector3.new(0, 1, 0))
local upVector = rightVector:Cross(lookVector)
local rayCFrame = CFrame.FromMatrix(pos, rightVector, upVector)

This discussion is preserved as it was. New answers, comments, and votes are closed.