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

What should I change in this to add bullet spread?

Asked by 9 years ago
local ray = Ray.new(tool.Shooter.CFrame.p, (mouse.Hit.p - tool.Shooter.CFrame.p).unit*300)

I'm stumped on what to change to make this bullet spread.

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

The Ray.new function has a syntax of;

Ray.new( Vector3 origin, Vector3 end)

So what you need to do is offset the look argument by a wanted minimum and maximum CFrame, using math.random.

math.randomseed(tick()) --to make it completely random

--Define offsets for each axis
local xMin,xMax = 0,10
local yMin,yMax = 0,10
local zMin,zMax = 0,10

--Define origin
local origin = tool.Shooter.CFrame

--Define look as mouse.Hit multiplied by randomized offsets
local look = mouse.Hit * CFrame.new(
    math.random(xMin,xMax),
    math.random(yMin,yMax),
    math.random(zMin,zMax)
)

--Create the ray
local ray = Ray.new(origin.p, (look.p - origin.p).unit*300)
Ad

Answer this question