I am trying to create a recoil function using cframe lerp, since from what ive seen, its either that or using a modulescript.
So for some reason, when I lerp the cframe of the camera it prevents the player from moving the camera around freely. I have been searching around for a while, but sadly with no luck. I think I might have some idea of what is causing the issue, but I can not figure out how to solve it whatever I do.
Here is an example:
My code:
function createRecoil(kickVer, kickHor)
local l1 = cam.CFrame * CFrame.Angles(math.rad(kickVer), 0, 0)
local l2 = cam.CFrame * CFrame.Angles(math.rad(kickVer/1.6), 0, 0)
coroutine.wrap(function()
for i = 0, 1, 0.1 do
cam.CFrame = cam.CFrame:Lerp(l1, i)
task.wait()
end
for i = 0, 1, 0.1 do
cam.CFrame = cam.CFrame:Lerp(l2, i)
task.wait()
end
end)()
end
You need to set the priority to directly after the default camera update which is Enum.RenderPriority.Camera.Value (which is 200). Then in your DoRecoil function, set the camera offset directly using cam.CFrame = yourRecoilCFrameOffset, then manually change yourRecoilCFrameOffset for use in the next render step update. Neither lerping nor tweening would work for this.
You’re lerping to the old camera’s cframe, i.e the l1 and l2 variables get assigned to the camera’s current cframe, and because of the task.wait, as time passes, as the player moves their camera, it changes the camera’s cframe, while the variables don’t change, and they just point to the old camera’s cframe that was assigned x seconds ago.
A solution would be to assign the l1 and l2 variables inside the for loops, so it always uses the current camera cframe.
function createRecoil(kickVer, kickHor)
task.spawn(function()
for i = 0, 1, 0.1 do
local l1 = cam.CFrame * CFrame.Angles(math.rad(kickVer), 0, 0)
cam.CFrame = cam.CFrame:Lerp(l1, i)
task.wait()
end
for i = 0, 1, 0.1 do
local l2 = cam.CFrame * CFrame.Angles(math.rad(kickVer/1.6), 0, 0)
cam.CFrame = cam.CFrame:Lerp(l2, i)
task.wait()
end
end)
end
I also took the liberty to change your coroutine.wrap to a task.spawn, 'cause i roll like that.