Hello. Currently, I am developing a third-person shooter game.
In my last thread, I got a nice introduction on moving projectiles. Cool. But, a projectile needs collision detection. I have heard, that using the touched event never should be used. So I tried these two things:
1. Use GetTouchingParts on Heartbeat
On Heartbeat, I always checked GetTouchingParts on the projectile.
2. Casting a ray every Heartbeat
Every Heartbeat, I casted a ray into the direction of the projectile’s lookVector and checked for the part on that.
Both of these methods worked fine, but had 1 problem:
It doesn’t recognize the collision instantly.
Look at this footage:
The bullet slides on the SpawnPoint for some time. As it might be something different on me, here’s my script:
function shootingSystem:Fire(gun, faceto)
--Placing and facing the projectile
local projectile = gun.Projectile:Clone()
projectile.CFrame = CFrame.new(gun.Model.Main.Position, faceto)
--Creating the BodyVelocity
local velocity = Instance.new("BodyVelocity")
velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
velocity.Velocity = projectile.CFrame.lookVector * gun.ProjectileSpeed
--Parenting the velocity
velocity.Parent = projectile
--Checking if the projectile is touching anything every heartbeat
local connection
connection = RunService.Heartbeat:Connect(function()
print("Heartbeat fired:".." "..tick())
local touching = GetTouching(projectile)
print("Got touching part: ".." "..tick())
if(touching ~= nil) then
--Something has been touched. Destroy the projectile and disconnect the event connection
print("Touched something on "..tick().."!")
connection:Disconnect()
projectile:Destroy()
end
end)
--Firing the projectile and removing it after it's lifetime
projectile.Parent = workspace
Debris:AddItem(projectile, gun.ProjectileLifetime)
end
Are there any other methods? Can I fix the ones I already tried? Is the touched event still unusable, or quite reliable nowadays? Thanks in advance - Sonnenroboter.