Hi,
I’ve been trying to add a gun recoil to my guns. But it breaks whenever you spam the left mouse button, it is multiplying the recoil onto itself and doesn’t stop. Does anybody know how to fix it or a better way of doing it? (I’m not asking for a complex recoil it’s just a simple camera wobble)
current code:
mouse.Button1Down:Connect(function()
-- checks if the tool is equipped --
if equipped == true then
if shooting == false then
-- checks if cooldown is over and there's ammo in mag --
if cooldown == false and magazineSize > 0 then
if automatic == true then
shooting = true
changePlrValue:FireServer("shooting", true)
while shooting == true do
-- checks if mag size is 0 --
if magazineSize == 0 then
gunState("No ammo")
else
cooldown = true
-- set gunState to "Ready" --
gunState("Ready")
-- fire server that the bullet was shot --
script.Parent.Fire:FireServer(mouse.Hit.p)
magazineSize -= 1 -- take one bullet from mag
-- set gunAmmo to current ammo out of mag size
gunAmmo()
-- recoil --
Connection = RunService.RenderStepped:Connect(playRecoil)
wait(recoilTime)
Connection:Disconnect()
-- wait fire cooldown --
wait(fireCooldown)
cooldown = false
end
wait(0.01)
end
end
(if the there’s not enough end’s it’s because I cut the code out)
recoil functions:
local function recoverRecoil()
Camera.CFrame = Camera.CFrame:Lerp(Camera.CFrame * CFrame.Angles(math.rad(-recoilHeight), 0, 0), incrementTime)
end
-- do camera recoil --
local function playRecoil()
local TotalSpeed = speedShake * tick()
local bobbleX = math.cos(TotalSpeed * distanceShake)
local bobbleY = math.abs(math.sin(TotalSpeed) * distanceShake)
Camera.CFrame = Camera.CFrame:Lerp(Camera.CFrame * CFrame.new(bobbleX, bobbleY, 0) * CFrame.Angles(math.rad(recoilHeight), 0, 0), incrementTime)
end
Thanks!