Help with shotgun bullet patern

I’ve been trying to make a shotgun bullet pattern for my fps using math.sin and math.cos but I cannot seem to get it working.

Here’s my code so far:

for i=1, Config.NumPellets.Value do
			local formula = math.pi*2/Config.NumPellets.Value*i --create formula for circle
			local x = math.cos(formula) * 0.1 --multiply by radius / spread
			local y = math.sin(formula) * 0.1 -- again
			local destination = Vector3.new(x,y,0) + mousepos --add circle to the mouseposition
			local ac = Cast:Fire(origin.Position,  (destination - origin.Position), velo, behavior) --fire with that position
			ac.UserData.Player = Player
		end	

However the circle never changes based on where the player is looking.

Video:

Any help is appreciated.

Instead of offsetting the destination along (1, 0, 0) for x and (0, 1, 0) for y, i.e. the world’s X and Y axis, you need to offset it along the muzzle’s local X and Y axes.

Try

...
local muzzleRight, muzzleUp = muzzle.CFrame.rightVector, muzzle.CFrame.upVector
local destination = mousepos + muzzleRight * x + muzzleUp * y
...

Thank you! This worked beautifully. For anyone wondering, here is my script:

for i=1, Config.NumPellets.Value do
			local formula = math.pi*2/Config.NumPellets.Value*i
			local x = math.cos(formula) * 0.1
			local y = math.sin(formula) * 0.1
			local muzzleRight, muzzleUp = viewmodel.Handle.GunFirePoint.WorldCFrame.rightVector, viewmodel.Handle.GunFirePoint.WorldCFrame.upVector ---get the viewmodel upvector and rightvector
			local destination = mousepos + muzzleRight * x + muzzleUp * y -- multiply these offsets, then add mousepos to create the circle at the mouses position
			local ac = Cast:Fire(origin.Position,  (destination - origin.Position), velo, behavior) --fire with FastCast
			ac.UserData.Player = Player
		end	
1 Like