Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How would I make a flawless collision system for projectiles?

Asked by
appxritixn 2235 Moderation Voter Community Moderator
4 years ago

I have tried to look through multiple forum pages, and they all say roughly the same thing. Part:GetTouchingParts() should return all of the colliding parts. However, this is not working for me. I would say that for me it works around 10% of the time. Here is my function to check for collision:

function checkCollision(part) -- new
   local connection = part.Touched:Connect(function() end)
   local results = part:GetTouchingParts()
   connection:Disconnect()
   return results
end

I am using connection to get around the problem of having a part which has CanCollide set to false, and trying to get the collision of that part.

And here is where I use the function (used when a spell is fired, until there are no more spells in the workspace).

game.Workspace.Spells.Slopigus.ChildAdded:Connect(function()
    local damage
    spells_ignis = game.Workspace.Spells.Ignis:GetChildren()

    while #spells_ignis > 0 do
        wait(0.15)
        for i,v in pairs(spells_ignis) do
            local collisionCheck = checkCollision(v)
            if #collisionCheck > 0 then
                for x,y in pairs(collisionCheck) do
                    for m=1,#playerParts,1 do
                        if y.Name == playerParts[m] then
                            local characterOfCollision = y.Parent
                            local nameOfCollision = characterOfCollision.Name -- Could be used for scoreboard updates
                            -- Deal appropriate damage to player
                            characterOfCollision.Humanoid:TakeDamage(damage) -- Damage is defined inside of the "Ignis" spell object
                            break
                        end
                    end
                end
                print("Collision")
                v:Destroy()
            else
                game.ReplicatedStorage.Debug.Recieve:FireAllClients()
            end
        end
    end
end)

I am not opposed to completely re-doing my script, but I would need to know what I am doing wrong, and what I should be doing.

All help is appreciated.

1 answer

Log in to vote
1
Answered by 2 years ago

An excellent method for detection collision for projectiles (which has come out since you have asked this question) is FastCastRedux.

It uses Raycasting and RunService to provide accurate collision detection, while minimizing the risks associated with lag.

Ad

Answer this question