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
1local 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.

01math.randomseed(tick()) --to make it completely random
02 
03--Define offsets for each axis
04local xMin,xMax = 0,10
05local yMin,yMax = 0,10
06local zMin,zMax = 0,10
07 
08--Define origin
09local origin = tool.Shooter.CFrame
10 
11--Define look as mouse.Hit multiplied by randomized offsets
12local look = mouse.Hit * CFrame.new(
13    math.random(xMin,xMax),
14    math.random(yMin,yMax),
15    math.random(zMin,zMax)
16)
17 
18--Create the ray
19local ray = Ray.new(origin.p, (look.p - origin.p).unit*300)
Ad

Answer this question