local module = {}
local rep = game:GetService("ReplicatedStorage")
local camManager = require(rep.CameraManager).Instance
local camera = game.Workspace.CurrentCamera
local ts = game:GetService("TweenService")
local angle = .6--.6
local upDuration = .05--.05
local downDuration = .08--.08
local rs = game:GetService("RunService")
function module:Recoil()
local neutral = CFrame.Angles(0,0,0)
local goal = CFrame.Angles(math.rad(angle),0,0)
local startTime = tick()
local conn
conn = rs.RenderStepped:Connect(function()
local alpha = math.clamp((tick()-startTime)/ upDuration, 0, 1)
local currentCFrame = neutral:Lerp(goal, alpha)
camManager:SetOffset("Recoil", currentCFrame)
if alpha >= 1 then
conn:Disconnect()
conn = nil
end
end)
repeat task.wait() until not conn
local startTime = tick()
conn = rs.RenderStepped:Connect(function()
local alpha = math.clamp((tick() - startTime) / downDuration, 0, 1)
local currentCFrame = neutral:Lerp(goal, alpha)
local x,y,z = currentCFrame:ToEulerAngles()
currentCFrame = CFrame.Angles(-x,-y,-z)
camManager:SetOffset("Recoil", currentCFrame)
if alpha >= 1 then
camManager:SetOffset("Recoil", nil)
conn:Disconnect()
conn = nil
end
end)
end
return module
Problem:
The recoil works but the camera doesn't come back to the exact spot that it was previously on. The player always ends up looking slightly higher every time the script runs.
I'd appreciate it if someone could explain why that's happening exactly and if there are some better ways of doing this.