Simple fast hit detection

I just want to share a simple code which detects objects moving at insane fast speeds and can check if it hit something.

Code:

local RunService = game:GetService("RunService")

local part = script.Parent
local previousPosition = part.Position

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

RunService.Stepped:Connect(function()
    local origin = part.Position
    local direction = (origin - previousPosition) * (origin - previousPosition).Magnitude
    local raycast = workspace:Raycast(origin, direction, raycastParams)

    if raycast then
        print("hit")
    end

    previousPosition = origin
end)

I’ve tested this by having a part super high in the sky with gravity set to 1000 and it detected.

1 Like