How would I make bullet spread?

Basically, I am currently making a tank smoke deploying system and I need the instanced “smoke bullets” to spread about 20 degrees from each other, how would I do that?

Script:

local CanSmoke = false
function CreateSmokeBombs(MouseHit)
	if CanSmoke == true then return end
	CanSmoke = true
	local SmokeTime = chassis.Config.SmokeTime.Value
	local SmokeSize = chassis.Config.SmokeSize.Value
	local SpawnPart = chassis.Body.SmokeSpawn
	local SpawnPart2 = chassis.Body.SmokeSpawn2
	
	
	
	for i = 1,12 do
		local SmokeBall = Instance.new("Part")
		SmokeBall.Name = "SmokeBall"
		SmokeBall.Color = Color3.fromRGB(163, 162, 165)
		SmokeBall.Material = Enum.Material.Metal
		SmokeBall.Shape = Enum.PartType.Ball
		SmokeBall.Size = Vector3.new(0.35, 0.35, 0.35)
		if i == 1 or i == 3 or i == 6 or i == 8 or i == 10 or i == 12 then
			SmokeBall.CFrame = SpawnPart.CFrame 
			SmokeBall.CFrame = CFrame.new(SmokeBall.Position,MouseHit.p) 
		elseif i == 2 or i == 4 or i == 5  or i == 7 or i == 9 or i == 11 then
			SmokeBall.CFrame = SpawnPart2.CFrame 
			SmokeBall.CFrame = CFrame.new(SmokeBall.Position,MouseHit.p) 
		end
		SmokeBall.Parent = workspace
		
		local BV = Instance.new("BodyVelocity",SmokeBall)
		BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BV.Velocity = SmokeBall.CFrame.lookVector * 50 + Vector3.new(0,20,0) 
		
		Debris:AddItem(BV,0.6)

I’ve tried many methods, however, none of them seems to work.
Thank you for reading or even answering :slight_smile:

I’m not really sure how to do what you’re asking but I can offer some efficiency advice, instead of doing elseif and checking every other number, you can just do else and it will account for those numbers not stated in the initial if statement.

I would be willing to bet that no if statement is actually needed inside this loop.

Instead of the long list of conditionals, you can just do if i % 2 == 1 and just an else statement.

2 Likes

A common method is to take the direction vector (unit vector of direction the gun is pointing) and add to it an offset vector whose length is the arctangent of half the desired spread angle, and whose direction is a random perpendicular to the direction vector. So, for example, if your spread angle is a 20-degree cone, your offset vector length would be math.atan(math.rad(10)).

There are a number of ways to get a random perpendicular to your direction vector, probably the easiest of which is to take the cross product with some other vector that is not colinear, such as whichever world axis farthest from your direction vector, normalize the result, and then give it a random spin, which is super easy since you can just multiply it by CFrame.fromAxisAngle( yourDirectionVector, RNG:NextNumber() * 2 * math.pi)

You’d generally do this only if odd/even was what mattered. His explicit numberlists are not separating odd and even numbers though. If the value of i doesn’t matter at all, there is probably no need for a comparison at all, not with i%2, i<7, i>6, etc… or anything else.

Oh, I didn’t notice earlier. I originally thought he wanted an odd-even pattern.