Question:
How do I detect when a object moving at a fast speed touches something?
Problem:
Touch event dose not fire when a object is moving at high speeds
Context:
I am making a Anti air craft gun.
Things I have tried:
RayCasts, fast object using the touched event
Requirements/Reason
- Bullet speed/ To make it harder to hit something far away
-Bullet Drop/ This would be for heavier Anti air weapons
-Bullet Visibility/ so players can correct their shots
Examples
-General Quarters(Has the anti air I am looking for)
- Naval Warfare(No bullet drop)
Instead of using the Touch event, you can try to use :GetPartsInPart()
workspace:GetPartsInPart(bullet,overlapParams)
They are more reliable than the touch event. I assuming you are using parts as your bullets since you want visuals and bullet drop (although I think this can be done with raycasts).
There are quite a few posts about this already.
I tried searching the forums for “how to detect fast objects” and there are a few posts about it.
Try also searching “touched event for projectiles”, “detecting bullet hits” or other similar search terms.
--detect all objects with speed above 100
local threshold = 100
local parts = {}
local function partAdded(part: BasePart): ()
if not part:IsA("BasePart") then return end
--small optimization so we only check physically active parts
if not part.Anchored then table.insert(parts, part) end
part:GetPropertyChangedSignal("Anchored"):Connect(function()
if part.Anchored then
table.remove(parts, table.find(parts, part))
elseif not table.find(parts, part) then
table.insert(parts, part)
end
end)
end
workspace.DescendantAdded:Connect(partAdded)
for _, descendant in pairs(workspace:GetDescendants()) do
task.spawn(partAdded, descendant)
end
local function getFastObjects(threshold: number): {BasePart}
local objects = {}
for _, part in pairs(parts) do
local speed = part.AssemblyLinearVelocity.Magnitude
if speed <= threshold then continue end
table.insert(objects, part)
end
return objects
end
game:GetService("RunService").Heartbeat:Connect(function()
local fastObjects = getFastObjects(threshold)
if #fastObjects == 0 then return end
print(fastObjects)
end)
Sorry that’s not what I meant. I wanted to detect when a fast moving object touches something.