im trying to make my gun system have random spread
i tried to use random.new():NextNumber() but i couldn’t figure it out
code in a server script
targetlocation is the mouse location
i used to just add the spread value to the targetlocation but that makes it the same at all ranges which is not what i want

```
local RayDirection = (TargetLocation - Handles.Position) * raynge
```

1 Like

tedi0usz
(tedious)
July 2, 2023, 11:31pm
#2
Hi, try this:

```
local Random = Random.new()
local spreadAngle = math.rad(spreadValue)
local randomSpreadAngle = Random:NextNumber() * 2 * math.pi
local spreadVector = Vector3.new(math.sin(randomSpreadAngle) * spreadAngle, 0, math.cos(randomSpreadAngle) * spreadAngle)
local rayDirection = (TargetLocation - Handles.Position).Unit + spreadVector
rayDirection = rayDirection.Unit
local finalDirection = rayDirection * raynge
```

1 Like

the system i use makes a part the length of the raycast but setting its cframe to finalDirection wont work

```
local Beam = Instance.new("Part", workspace)
Beam.BrickColor = BrickColor.new("New Yeller")
Beam.FormFactor = "Custom"
Beam.Transparency = 0
Beam.Material = "Neon"
Beam.Anchored = true
Beam.CanCollide = false
-- Math.
local Distance = (Handles.Position - TargetLocation).magnitude
Beam.Size = Vector3.new(0.075, 0.075, Distance)
Beam.CFrame = CFrame.new(Handles.Position, TargetLocation) * CFrame.new(0, 0, -Distance/2)
game.Debris:AddItem(Beam, 0.05)
```

I got back to testing and I believe this does not work

Hi, after some more rigorous testing, i got this working, however, the vertical spread only kicks in when aiming above or below the starting position. how can i make it always spread vertically?

tedi0usz
(tedious)
May 30, 2024, 3:26am
#6
tbh i dont remember, but try this

```
local Beam = Instance.new("Part", workspace)
Beam.BrickColor = BrickColor.new("New Yeller")
Beam.FormFactor = "Custom"
Beam.Transparency = 0
Beam.Material = "Neon"
Beam.Anchored = true
Beam.CanCollide = false
local Handles = Vector3.new(0, 0, 0)
local TargetLocation = Vector3.new(10, 5, 10)
local Distance = (Handles - TargetLocation).magnitude
Beam.Size = Vector3.new(0.075, 0.075, Distance)
local Midpoint = (Handles + TargetLocation) / 2
local VerticalOffset = Vector3.new(0, Distance/2, 0)
Beam.CFrame = CFrame.new(Midpoint, TargetLocation) * CFrame.new(0, 0, -Distance/2) * CFrame.new(VerticalOffset)
Beam.CFrame = Beam.CFrame * CFrame.Angles(math.atan2(TargetLocation.Y - Handles.Y, (TargetLocation - Handles).magnitude), 0, 0)
game.Debris:AddItem(Beam, 0.05)
```

thank you for replying, but i found a solution

```
if sprad == nil then
sprad = 0
end
local Random = Random.new()
local Offset = Vector3.new(
math.random(-sprad,sprad),
math.random(-sprad,sprad),
math.random(-sprad,sprad)
)/100 * .25
local spreadAngle = math.rad(sprad)
local randomSpreadAngle = Random:NextNumber() * 2 * math.pi
local spreadVector = Vector3.new(math.sin(randomSpreadAngle) * spreadAngle, 0, math.cos(randomSpreadAngle) * spreadAngle)
local rayDirection = (TargetLocation - Handles.Position).Unit + Offset
rayDirection = rayDirection.Unit
local finalDirection = rayDirection * raynge
```

system
(system)
Closed
June 16, 2024, 1:21am
#8
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.