How would i add spread to this projectile?

I’ve seen some other posts but didnt get it to work, so if someone could help me add spread to these projectiles i would be very thankful.

local attackmodule = {}

local attacks = require(script.AttackPatternsModule)

local projectiles = game.ServerStorage.Boss.Stuff.Projectiles
local star = projectiles.StarProjectile
local rns = game:GetService("RunService")

function attackmodule.Attack(attack, boss)
	if table.find(attacks, attack) ~= nil then
		attackmodule.UseAttack(attack, boss)
	end

end

function attackmodule.UseAttack(attack, boss)
	if attack == "CrystalBarrage" then
		print("Boss used Crystal Barrage.")
		boss.CanAttack.Value = false
		print(boss.CanAttack.Value)


		for i=1, math.random(9,12) do
			task.wait((math.random(2,3))/15)
			
			local steps = 0
			
			coroutine.wrap(function()
				
				local proj = star:Clone()
				proj.Parent = workspace.Debris
				proj:MoveTo(boss.PrimaryPart.Position+Vector3.new(math.random(-4, 4),7,math.random(-4, 4)))

				local nearestPlayer, nearestDistance
				for _, player in pairs(game.Players:GetPlayers()) do
					local character = player.Character
					local distance = player:DistanceFromCharacter(boss.HumanoidRootPart.Position)
					if not character or 
						(nearestDistance and distance >= nearestDistance)
					then
						continue
					end
					nearestDistance = distance
					nearestPlayer = player
				end
				if nearestPlayer ~= nil then
					local nhrp = nearestPlayer.Character.HumanoidRootPart
					print(CFrame.lookAt(boss.PrimaryPart.Position,nhrp.Position))
					proj.PrimaryPart.CFrame = CFrame.lookAt(boss.PrimaryPart.Position,nhrp.Position)
				end
				
				local rssteps
				rssteps = game:GetService("RunService").Heartbeat:Connect(function()
					steps += 1
					proj.PrimaryPart.Position = proj.PrimaryPart.Position + proj.PrimaryPart.CFrame.LookVector * 1
					
					if steps >= 150 then 
						proj:Destroy()
						rssteps:Disconnect()
					end
					
				end)
				return
		end)()
	end
end
task.wait(0.4)
boss.CanAttack.Value = true
end


return attackmodule


This is generally what I do:

local spread = 5 -- the amount of spread you want
local fireOrigin = Vector3.new(0, 3, 0) -- the starting position of the projectile
local fireDirection = Vector3.new(0.5, 0.2, -0.7) -- the direction the projectile will fly in
local randomizedVector = Vector3.new(math.random(-spread, spread), math.random(-spread, spread), math.random(-spread, spread))
local newDirection = (fireDirection + randomizedVector - fireOrigin).Unit

-- fire the projectile using the new direction

I know there are better methods, but this is what works for me.

I dont think this would work since im using CFrames sadly.

1 Like

It would work, as CFrame is designed for describing a position with a reference axis. In this case, you already have the orientation, it is simply the direction of the bullet, but naturally the bullet comes from the gun muzzle, so you have everything you need.

Construct a CFrame facing newDirection, e.g. CFrame.lookAlong(muzzle.Position, newDirection)

Not really, as there is not much to spread as a mechanic. Recoil however, there is much to be said about recoil.

1 Like