How would i get parts surface independent of parts orientation?

so i have this script that detects which surface of a part players mouse is on, but there is one problem. the surface is dependent of parts orientation. what i mean is like if you rotate the part by 180 degrees in the Y axis, its gonna say bottom. and what i want to achieve is detecting the surface independent of the parts rotation. here is a picture. i hope you understand me

norm = hit.CFrame:vectorToObjectSpace(norm)
		local nx,ny,nz = norm.X, norm.Y, norm.Z
		if nx ~= 0 and (ny == 0 and nz == 0) then
			if nx > 0 then
				print("Front")
				x += hitbox.Size.X/2
			else
				print("Back")
				x -= hitbox.Size.X/2
			end
		elseif ny ~= 0 and (nx == 0 and nz == 0) then
			if ny > 0 then
				print("Top")
				y += hitbox.Size.Y/2
			else
				print("Bottom")
				y -= hitbox.Size.Y/2
			end
		elseif nz ~= 0 and (nx == 0 and ny == 0) then
			if nz > 0 then
				print("Left")
				z += hitbox.Size.Z/2
			else
				print("Right")
				z -= hitbox.Size.Z/2
			end
		end

Assuming that you’re raycasting, use the direction of the surface normal.

For example, a surface normal around 0, 1, 0 is the top surface. 0, -1, 0 is the bottom, etc.

either i dont understand you or you dont understand me. i know how to get the top surface but its dependent of the parts orientation so if the part is flipped, the top surface is on the bottom of the part

A surface normal stays the same no matter how the part is orientated…

The raycast returns the normal of the surface hit, meaning that part orientation doesn’t matter.

If you raycasted downwards and hit the top surface of a perfectly cubic part, the surface normal would point up, 0, 1, 0. Even if you flipped that part 180 degrees meaning that you’d be hitting it’s bottom surface, the surface normal is still 0, 1, 0 because the surface relative to the ray is still 0, 1, 0

okay i think im starting to understand now

Illustration of what I mean:

The surface normals are always pointing up (visualize the down arrows you drew as rays). The blue arrows are the surface normals

didnt understand you at first, but yeah thats working, thanks!

1 Like