How would i go about making a gun spread like this?

Hello. I’m making a 2d game with guns and I can not figure out how to make a gun spread like this:

This is the script right now:

local remote = script.Parent:WaitForChild("Raycast")

remote.OnServerEvent:Connect(function(player, position)
	for i=1,10 do
	local hrp = player.Character.HumanoidRootPart
	
	local origin = hrp.Position
	local direction = (position - origin).Unit*300
	local result = workspace:Raycast(origin, direction)

	local intersection = result and result.Position or origin + direction
	local distance = (origin - intersection).Magnitude
	
	local bullet_clone = Instance.new("Part")
	bullet_clone.Name = "Ray"
	bullet_clone.Anchored = true
	bullet_clone.CanCollide = false
	bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
	bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
		bullet_clone.Parent = workspace
		
		game.Debris:AddItem(bullet_clone,.25)

	if result then
		local part = result.Instance
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

		if humanoid then
			humanoid:TakeDamage(10)
		end
	end
	end
end)

If you have any ideas on how to make this , please reply to my post.

Try adding the spread with CFrame.Angles

bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)*CFrame.Angles(verticalSpread, horizontalSpread, 0)

I’ve been used this code and this solved my problem.

local d = Direction + Vector3.new(math.random() * 45, math.random() * 45, math.random() * 45)

on your case i think that u only need the remove the y from the vector.

Hope that this helped!

I solved this myself after 3 days so no need to reply anymore.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.