TargetSurface for Rays?

I have a question:
Is there a method or anything else which returns the face of a part my ray is pointing at, like Mouse.TargetSurface property but for Ray casting?

I am trying to get the Face or NormalId of a part my ray is pointing at.
I have tried using the Normal and LookVectors or Dot products and matching the angles but I just can’t figure out how it can help me.

I am looking forward for answers, thank you.

1 Like

RaycastResult will return RaycastResult.Normal, which will give you the normal vector of the face that is intersected by the Ray.

https://developer.roblox.com/en-us/api-reference/datatype/RaycastResult

Here’s a solution

I just found a Solution from the script
(WorldPositionToSurfaceGui - Roblox)

But your solution works too.
Thank you!

1 Like

For those wondering, the solution I found takes a Position and returns the closest surface form a Part.
it goes like this:

local function GetSurface(Part,Position)
		local rel = Part.CFrame:pointToObjectSpace(Position) / Part.Size;
		local relX = rel.X;
		local relY = rel.Y;
		local relZ = rel.Z;
	    local absX = math.abs(relX);
	    local absY = math.abs(relY);
	    local absZ = math.abs(relZ);

		if (absZ > absY) and (absZ > absX) then
			if (relZ > 0) then
				return Enum.NormalId.Back
			else
				return Enum.NormalId.Front
			end
		elseif (absY > absZ) and (absY > absX) then
			if (relY > 0) then
				return Enum.NormalId.Top
			else
				return Enum.NormalId.Bottom
			end    
		elseif (absX > absZ and absX > absY) then
			if (relX > 0) then
				return Enum.NormalId.Right
			else
				return Enum.NormalId.Left
			end    
		end

		return nil
	end
1 Like

i need for rays and it says there is no positions on rays :D.