Adding recoil on a gun using a contradicting camera loop

I’m trying to add recoil to a gun every time you fire it. However there is a RenderStepped loop that is contradicting the recoil which is used to control the over the shoulder camera of the gun, which always puts the camera back to it’s original position. I want the recoil to tween or lerp and not happen instantly, however I don’t want it to do so all the time because then the camera wouldn’t follow as fast. Currently I’m not sure how to integrate that into my code.

local bloomValue = CFrame.new(0, 0, 0)

local function cameraBloom(status) -- everytime you fire the gun
	if status == true then
		
		local randomCFrame = CFrame.new(math.random(0, 1) / 5, math.random(0, 1) / 5, 0)		
		
		bloomValue = randomCFrame
		
	elseif status == false then
		
	end
end

connection = game:GetService("RunService").RenderStepped:Connect(function()

		startCFrame = CFrame.new((hrp.CFrame.Position)) * CFrame.Angles(0, math.rad(x), 0) * CFrame.Angles(math.rad(y), 0, 0)

		cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(offset.X, offset.Y, offset.Z))
		cameraDirection = startCFrame:ToWorldSpace(CFrame.new(offset.X, offset.Y, -10000)) 

		camera.CFrame = CFrame.new(cameraCFrame.Position, cameraDirection.Position) * bloomValue
		hrp.CFrame = CFrame.lookAt(hrp.Position, Vector3.new(cameraDirection.Position.X, 0, cameraDirection.Position.Z))
		
	end)

Solved by rewriting most code and switching the camera loop to Humanoid.CameraOffset

1 Like