Ray spread doesn't work as expected, why?

  1. What do you want to achieve? I want to make a gun that shoots a ray that has a random spread

  2. What is the issue? For some reason whenever I shoot the gun (I looped the raycast 6 times for testing purposes) the outcome is this:
    image
    I’ve noticed that sometimes two rays go to the near exact same place and for some reason no matter which direction I face all the rays stack on top of each other.

  3. What solutions have you tried so far? I got the code for the random spread from this post:
    How to spread like a shotgun with direction? (new raycast system) - #5 by rokoblox5

This is the code used for the raycasting (spread is 5)

for i=1,6 do 
	-- credit to rokoblox5 for this great thing
		local ray = CFrame.new(origin, origin + direction) * CFrame.fromEulerAnglesXYZ(math.rad(math.random(0, spread)), 0, math.rad(math.random(0,360)))
		local rayResult = game.Workspace:Raycast(ray.Position, ray.LookVector*distance, rayParams)

I believe the issue is caused by setting the spread value to low, this spread value controls the horizontal left and right spread using the EulerAngles CFrame math formula. However the vertical up and down spread which is:

Remains the same, hence the bullets will stack up and down on top of each other. Perhaps try replacing 360 with the spread value,

Alternatively you can use QuasiDucks method here which also does circular spread. The current method can work as well but well its up to you.

1 Like