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

Why is my bullet spread the same regardless of distance?

Asked by
gitrog 326 Moderation Voter
7 years ago

So, I've written a bullet spread script, but there's a problem with it. No matter the range, the bullet will offset the same amount, making it possible to miss at point blank which obviously is an issue.

Here's my script, any ideas how to fix it?

    local xOff = math.random(-spread * 100, spread * 100)
    xOff = xOff/100
    local yOff = math.random(-spread * 100, spread * 100)
    yOff = yOff/100 
    local zOff = math.random(-spread * 100, spread * 100)
    zOff = zOff/100 

    local NewPos =  mouse + Vector3.new(xOff, yOff, zOff)
    local ray = Ray.new(tool.firePart.CFrame.p, (NewPos - tool.firePart.CFrame.p).unit*300) 
    local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList, false, true)

1 answer

Log in to vote
1
Answered by 7 years ago

No wonder. Your script adds the spread to an already existing position.

You could try using trigonometry. But an easier way would be to multiply the base offset by the magnitude and then divide it by a constant.

0
Hm. That's what I thought of doing, but say I aimed for the edge of a wall, and the spread made it not hit the wall. Wouldn't the magnitude be the magnitude of the wall? gitrog 326 — 7y
0
You should use rays for this; otherwise, your script won't work one way or another. The hit position is nothing but a direction for them, so they'll continue past it. nooneisback 81 — 7y
Ad

Answer this question