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

How to detect collisions when tweening part positions?

Asked by
gitrog 326 Moderation Voter
5 years ago

So, I've been trying to make a script where the bullets can be seen whizzing through the air if one looks closely, and have more in depth mechanics such as penetration. First I tried velocity, but it was very laggy (lots of rubber-banding) and the bullets didn't always make contact for the faster ones.

Now, I'm trying tweening the position, but the "Touched" event will not fire at all, even at normal speeds (player walking). Any tips for how to detect collisions or a better way to do this?

--This is a small segment of my script that is used to calculate the bullet's movement

    --Begin the tweening of the bullet
    while velocity > 32 and bullet.Position.y > -100 do
        --Set the target position to move towards
        local targPos = bullet.CFrame * CFrame.new(velocity,-3.2,0)

        --Tween the bullet
        local tweenShot = TweenService:Create(bullet,TweenInfo.new(1),{CFrame = targPos}):Play()

        bullet.Touched:Connect(function()
            --Determine what the bullet hit
            if debounce then
                debounce = false
                --Stop the bullet to perform calculations
                tweenShot:Cancel()
            end
        end)

        velocity = velocity - 5
        wait(1)
    end

Answer this question