Still have questions? Join our Discord server and get real time help.
Log in to vote

How can I detect a surface a part hit?

Asked by
Dominical 210 Moderation Voter
4 years ago

Hello! I've been wondering on how I could detect which surface a bullet(part) hit. I've searched the wiki without finding an efficient solution. I could use TargetSurface but that can't return the surface the actual bullet hit. I apologize for lacking a bit of info or code if I am.

local bullet=script.Parent
if hit:IsA("Part") then
-- detect surface; how can I achieve this?

If this is not possible in Roblox, I apologize as well. I appreciate any help!

GUI's are able to do this, I dont see why a part cant.. MessorAdmin 560 — 4y
I would like to know as well. Perci1 4905 — 4y

1 answer

Log in to vote
Answered by 4 years ago

You could do something like check which side of the part is in closest proximity to the bullet. For this, you'd have to write this thingy here:

--(Assuming the script is within the bullet)

local bullet = script.Parent

if hit:IsA("Part") then

local sides = {}
local sizes =,hit.Size.Y,hit.Size.Z)
local pos = hit.Position
sides[1] = pos + hit.CFrame:vectorToWorldSpace(,0,-hit.Size.Z/2))--front
sides[2]= pos + hit.CFrame:vectorToWorldSpace(,0,hit.Size.Z/2))--back
sides[3] = pos + hit.CFrame:vectorToWorldSpace(,0,0))--right
sides[4] = pos + hit.CFrame:vectorToWorldSpace(,0,0))--left
sides[5] = pos + hit.CFrame:vectorToWorldSpace(,hit.Size.Y/2,0))--top
sides[6] = pos + hit.CFrame:vectorToWorldSpace(,-hit.Size.Y/2,0))--bottom

local ClosestSide = 1
for i,v in ipairs(sides) do
if (bullet.Position - v).Magnitude < (bullet.Position - sides[ClosestSide]).Magnitude then
ClosestSide = i

if ClosestSide == 1 then print("front")
elseif ClosestSide == 2 then print("back")
elseif ClosestSide == 3 then print("right")
elseif ClosestSide == 4 then print("left")
elseif ClosestSide == 5 then print("top")
elseif ClosestSide == 6 then print("bottom")

front = negative z back = positive z right = positive x left = negative x top = positive y bottom = negative y

Now unfortunately, this is only accurate when used on on cubes, or other objects with symmetrical sizes. Don't worry though, hopefully I'll be able to edit this answer later and provide a better solution.

I'll also adjust the script later so you can actually reference the side.


Answer this question