Shotgun Recoil Problem

I’ve got this shotgun tool. The shotgun effect is done by for i = 1,BulletCount do. The problem is that this duplicates the recoil code which is done by Camera.CFrame = Camera.CFrame * CFrame.Angles(0.3,math.rad(0),0). If I combine both of them, the gun’s range is screwed up (100 studs becomes 10), and the recoil duplicated like crazy. I’ve tried placing the recoil code behind the fire animation line and then having the shotgun code go after the recoil code. That forces the player’s camera up, but since the shotgun code is after the recoil code, the pellets go to where the cursor is now. What should happen is the bullet is where the cursor was originally, but the cursor/gun is now up.
Help?

if MyMouse then
				Camera.CFrame = Camera.CFrame * CFrame.Angles(0.3,math.rad(0),0)
				RecoilTrack:Play()
				
				for i = 1,BulletCount do -- shotgun/burst effect
					
					
				local targetPoint = MyMouse.Hit.p
				local shootDirection = (targetPoint - Handle.Position).unit

					
					
				shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread) * shootDirection

Before I spend time trying to draft a solution, I would just like some clarification.

You want the bullets to shoot towards the ORIGINAL cursor position BEFORE recoil, correct? Or do you want them to shoot to the mouse’s position AFTER the recoil?

1 Like

Why cant you just fire the bullets first then add the recoil?

1 Like

Can’t you just move those first two lines to run after the for-loop?
You’re also assigning targetPoint every iteration of the loop - realistically once you shoot your target is already set, not changing as the gun kicks. I’d create targetPoint once before the for-loop

1 Like

It’s not my code, its a free model. but I think if MyMouse then is when the bullet is fired

I want the pellets to go to where the curser was originally at before being forced up by the recoil, so yes the first option

Ok, so I take it since it’s not your code you’re not fully familiar with what is happening in it.
MyMouse is a reference to game:GetService("Players").LocalPlayer:GetMouse() somewhere above in your code, as you’re calling MyMouse.Hit.P, using the “Hit” function.

Camera.CFrame = ... is making your camera jump upward.
RecoilTrack:Play() is either an AnimationTrack or a sound, the prior most likely. It makes the gun move.
for i = 1, BulletCount do is making your shotgun behave like one by calculating many small pellets as its projectiles rather than a single bullet.
targetPoint is the physical position at which your mouse is pointing at
shootDirection is calculating the line in-between your gun and the thing it’s point at, and converting it to a Unit vector.
shootDirection = CFrame.Angles... is adding some variety to the spread pattern of the bullets by randomizing the unit vector a little.

You could fix this code to how you want it in two ways, the easiest (but perhaps least realistic looking) would to move these two lines:

Camera.CFrame = Camera.CFrame * CFrame.Angles(0.3,math.rad(0),0)
RecoilTrack:Play()

to the very end of this if myMouse then... code, as there’s more beyond what you’ve snipped into your post involved in making the gun fire. I would imagine all of it is happening in the for i = 1, BulletCount do.. part, so move them somewhere after that.

The other way would be to move the part where you’re capturing targetPoint and shootDirection to outside of the loop AND before you move the camera upward, so that the loop keeps referencing one static value rather than recalculating it each pellet.

1 Like

you mean like this?

	if MyMouse then
				
				RecoilTrack:Play()
				local targetPoint = MyMouse.Hit.p
				
				local shootDirection = (targetPoint - Handle.Position).unit
				
				Camera.CFrame = Camera.CFrame * CFrame.Angles(0.3,math.rad(0),0)
				
				for i = 1,BulletCount do -- shotgun/burst effect
					
			

				
				shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread) * shootDirection
				
1 Like