Help making gun recoil

Hello, I’m trying to add recoil to my gun, however I’m running into some problems.

here’s the code I use to add the recoil:

local pos = game.Workspace.CurrentCamera.CFrame
local tween = tweenservice:Create(game.Workspace.CurrentCamera, TweenInfo.new(0.1), {CFrame = pos * CFrame.Angles(math.rad(1),0,0) })
tween:Play()

It words- sort of. It doesn’t let the player move their camera to the side while its being shot and it is overall unstable.

try

--local script--
local run=game:GetService'RunService'
local cas=game:GetService'ContextActionService'
local recoilCF=CFrame.Angles(0,0,0)

local cam=workspace.CurrentCamera

run.RenderStepped:Connect(function()
   local smoothSpeed=.1
   recoilCF=recoilCF:Lerp(CFrame.Angles(0,0,0),smoothSpeed)
    --[[ smoothSpeed for smoothness, more precisely for smoothness after the shot
     Increase the value of smoothSpeed to make the smoothness fast and
      vise versa 
    --]]
   cam.CFrame*=recoilCF
end)

function recoil()
     local strength=1
     local speed=.8
     recoilCF=recoilCF:Lerp(CFrame.Angles(0,strength,0),speed)
     --[[ return recoil if you want
     spawn(function() -- not to overlap 
         task.wait(.2)
         recoilCF=recoilCF:Lerp(CFrame.Angles(0,-strength,0),speed)
     end)
   --]]
end


4 Likes

don’t put the strength variable on the Y axis because of the Y axis in CFrame.Angles act like the X axis.

I tried to add :
local physics = game.Workspace.CurrentCamera:GetPropertyChangedSignal(“CFrame”)
physics:Connect(function()
game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame:lerp(pos, 0.5)
end)

It works, but it takes a while to get back to the original position and it’s very slow.

//devforum-uploads.s3.dualstack.us-east-2.amazonaws.com/uploads/original/4X/e/9/5/e950306158014d9729e7f76ee4536fee25f3f1a5.mp4

The weird thing is that I used to use this code:
local physics = game.Workspace.CurrentCamera:GetPropertyChangedSignal(“CFrame”)
physics:Connect(function()
game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame:lerp(pos, 0.5)
end)

In my old game and it worked perfectly.
//devforum-uploads.s3.dualstack.us-east-2.amazonaws.com/uploads/original/4X/4/b/9/4b9acd8a7f5f0cd4a7c2f4b4e4c4a958e9b2633b.mp4

I have no idea what I did wrong.