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

Help Finding The Hit Surface/Axis?

I need to know on which surface/axis of the part a ray contacted with. Previously I used the surface normal and through some testing found the relationship between the surface and returned normal Vector. However when I tried the same thing with a rotated part, it no longer returns values between -1 and 1. What is the math to find the surface when it a part is rotated? RayCast is a basic raycasting function that returns that part hit, position of the hit, and the surface normal.

local hit,position,norm = RayCast(startPos,direction,tool.Parent)
if hit then
	wait()
	print(norm)
	local x,y,z = norm.X, norm.Y, norm.Z
	if x ~= 0 and (y == 0 and z == 0) then
		hitx = true
	elseif y ~= 0 and (x == 0 and z == 0) then
		hity = true
	elseif z ~= 0 and (x == 0 and y == 0) then
		hitz = true
	end
end

1 answer

✓ Accepted answer

Since you're given a unit vector for a rotated part, it's easier to convert the given Vector3 to object coordinates. Then, your original comparison works fine:

local hit, position, norm = RayCast(startPos, direction, tool.Parent)

if hit then
	wait()
	print(norm)
	norm = hit.CFrame:vectorToObjectSpace(norm) --This is where the magic happens.
	local x,y,z = norm.X, norm.Y, norm.Z
    if x ~= 0 and (y == 0 and z == 0) then
        hitx = true
	if x > 0 then
		print("Front")
	else
		print("Back")
	end
    elseif y ~= 0 and (x == 0 and z == 0) then
        hity = true
	if y > 0 then
		print("Top")
	else
		print("Bottom")
	end
    elseif z ~= 0 and (x == 0 and y == 0) then
        hitz = true
	if z > 0 then
		print("Left") --These two might be backwards!
	else
		print("Right")
	end
    end
end

3 comments

0

Wait so what did that do exactly?

0

If you look at the methods section of the CFrame page on the wiki, you'll get their explanation (as well as all the rest of the methods.) Basically, the unit vector is rotated as if the part itself is not rotated at all.

0

Oh nice! Thanks for the help

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