How to check if a point in 3D space intersects a part

I’m trying to write a script that detects if a point in 3D space intersects a part, but I can’t figure out exactly how to deal with rotation, I want to avoid creating new parts to mark the point if possible. Any help and/or guidance is appreciated.

Get the relative offset between the point in space and the Part’s CFrame. Then check if this is within the bounds of the Part (+/- half the Part’s Size in each axis):

function isPointWithinPart(part: Part, point: Vector3): boolean
   local halfSize: Vector3 = part.Size / 2
   local relativeOffset: Vector3 = part.CFrame:PointToObjectSpace(point)
   local absOffset: Vector3 = relativeOffset:Abs() --Take the absolute value of the offset because we only care about relative distance, not direction
   if absOffset.X > halfSize.X or absOffset.Y > halfSize.Y or absOffset.Z > halfSize.Z then
      return false
   else
      return true
   end
end

local part: Part = workspace.Part --Some path to this part
local point: Vector3 = Vector3.new(0, 0, 0) --Point in 3D space

local bool: boolean = isPointWithinPart(part, point)

1 Like

Wow that was fast, thanks mate!

This wouldn’t be that good for detailed meshparts. Alternatively you can use

workspace:GetPartBoundsInRadius()

with a very small radius and check if the wanted part is in the returned table. The detail would depend on the part’s collision properties.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.