Recoil using springs

Currently, I’m using this spring module to simulate stuff such as gun sway or bobbing. It works perfectly fine however my attempts at implementing gun recoil aren’t working.
SpringModule.lua (1.3 KB)

My current attempt at creating recoil is just a rehashed version of my gun bobbing code.

function ToolModule:VisualizeFiring(dt)
	-- Get these values from a table storing the info
	local RecoilTable = self.VisualValues:GetValue("Recoil")
	local X,Y,Z = RecoilTable.X, RecoilTable.Y, RecoilTable.Z
	local Rate, Power = RecoilTable.Rate, RecoilTable.Power
	
	local function Bobbing(addition)
		return math.sin(tick() * addition * Rate) * Power -- Rate, power
	end
	local bob = Vector3.new(Bobbing(X),Bobbing(Y),Bobbing(Z))
	self.RecoilSpring:shove((bob / 35) * dt * 60)
	local UpdateRecoil = self.RecoilSpring:update(dt)
		
	self.ViewModel.Torso.CFrame = self.ViewModel.Torso.CFrame:ToWorldSpace(CFrame.new(UpdateRecoil.X, UpdateRecoil.Y, UpdateRecoil.Z))
end
2 Likes

so where does it ‘not work’.
For example does the Bobbing function return a float correctly for each of the X, Y and Z values?
What is the value of UpdateRecoil for the values supplied by the VisualValues:GetValue(“Recoil”)?
Try putting some prints in so you can trace the values being used.

Let me rephrase myself, the code itself “works”, however, this implementation of recoil just doesn’t seem correct.
What I’m asking for is a correct implementation of gun recoil using the spring module in my OP, as of now the gun model only shakes on screen.

Bumping the post, any suggestions are appreciated.

Never mind I solved it (or at least somewhat).
Referring to BlackShibe’s FPS framework, I modified his recoil code so that it would change the CFrame position of the viewmodel, instead of the camera angle. It doesn’t look perfect but it can be tweaked to look better.

function ToolModule:VisualizeFiring(dt, Fired)
	if Fired then
		self.RecoilSpring:shove(Vector3.new(0, 0.05, 0.1) * dt * 60)
	
		task.delay(0.15, function()
			self.RecoilSpring:shove(Vector3.new(0, -0.05, -0.1) * dt * 60)
		end)
	end
	local recoil = self.RecoilSpring:update(dt)
	self.ViewModel.Torso.CFrame = self.ViewModel.Torso.CFrame:ToWorldSpace(CFrame.new(recoil.x, recoil.y, recoil.z))
end)
2 Likes