Spread On Raycast?

I am wondering how I could add random spread to a raycasting gun?

	-- Define Some Things
	local StudTravel = 250
	local StartPosition = ShootPoint.Position
	local Direction = (Position - StartPosition).Unit
	local EndPoint = workspace:Raycast(ShootPoint.Position, Direction*StudTravel)

	-- Define Some Things
	local Intersection = EndPoint and EndPoint.Position or (StartPosition + Direction*StudTravel)
	local Distance = (StartPosition - Intersection).Magnitude

	-- Make Bullet
	local NewBullet = MakeBullet:Clone()
	NewBullet.Size = Vector3.new(0.1, 0.1, Distance) -- Distance
	NewBullet.CFrame = CFrame.new(StartPosition, Intersection)*CFrame.new(0, 0, -Distance/2)
	NewBullet.Parent = game.Workspace
1 Like
--Add a random vector to direction
local Offset = Vector3.new(
   math.random(-100,100),
   math.random(-100,100),
   math.random(-100,100)
)/100 * 0.25

Direction += Offset

Just know that there’s nothing more annoying to a player than a gun that doesn’t shoot straight.
Take a look at the game Fist Full of Frags for how it handles accuracy.

Its arcadey, but they’re a skill to aiming correctly.

6 Likes

Thanks.

I’m using it for a shotgun but I can’t seem to actually get the spread? I can’t seem to find the problem.

On a side note may be related. If I try running back and shooting the ray stops, can this all be something related to ignorelist? I don’t think I have one.

Here’s my code

1 Like

You need to actually calculate a different offset for each bullet, to which you should calculate inside a for loop.

1 Like

It’s not shown on the screen, but i’m using a for loop and running this function for each bullet. Is that the same thing? It was working every now and then (every 10 attempts), weird.

I was going to say that it could be this


But probs not, here is something to try, maybe make the spread a bit larger

3 Likes

Yeah, that worked, thanks.

Pushed it up to 250, working without error now.