How to get CFrame from RaycastResult

I need to get a CFrame from ray. I used Mouse.hit but I need to use the ignore list so I must use FindPartOnRayWithIgnoreList, but it gives me only position. Any ideas?

Are you using Ray.new() or workspace:Raycast()?

FindPartOnRayWithIgnoreList does not return any CFrame values.


What you could do is this:

local ray = Ray.new()
local hit, hitpos = workspace:FindPartOnRayWithIgnoreList()
local DesiredCFrame = CFrame.new(hitpos, hitpos + CFrame.new(ray.Origin, ray.Direction).LookVector)

CFrames have a constructor where we give it 2 Vector3 values. The first is where the position is. The second Vector3 is the position you want the CFrame to look at. Let’s imagine it like this:

CFrame.new(Param1, Param2)
Param1 = WHAT POSITION SHOULD I BE AT?
Param2 = WHAT POSITION SHOULD I LOOK AT?

Obviously, you want the CFrame to be at where the ray hits, so set Param1 to the hitpos. For Param2, I create a CFrame at the ray’s origin, pointing at the ray’s direction, I take that CFrame’s LookVector, and I add it to hitpos, giving you the desired effect.

1 Like