Reversing a recoil that uses :Lerp()

How would I reverse this camera recoil that uses Lerp() like I would in a tween?

I figured using goal:Inverse() would work but it didn’t. Any way I can reverse this?

local recoil = CFrame.new()
		Recoiling = true
		
		local con; con = game["Run Service"].RenderStepped:Connect(function()
			local randomX = math.random(1.5, 3)
			local randomY = ((math.random(1, 2) == 1) and 1.5) or -1.5
			
			local goal = CFrame.Angles(math.rad(randomX), math.rad(randomY), 0)
			
			recoil = recoil:Lerp(goal, 0.05)
			Camera.CFrame *= recoil
			
			task.wait(0.1)
			recoil = recoil:Lerp(goal:Inverse(), 0.05) -- Attempt to reverse
			Camera.CFrame *= recoil
			
			task.wait(0.1)
			if (con) then
				Recoiling = false
				con:Disconnect();
			end
		end)

What is happening right now: Watch Day In The Life_ Grimwood - Roblox Studio 2024-05-19 21-12-01 | Streamable

1 Like

Lerp recoil to 0, not goal:Inverse()

Lerping it to 0 does nothing. Here’s what I’m doing

task.wait(0.1)
recoil = recoil:Lerp(CFrame.Angles(0, 0, 0), 0.05)
Camera.CFrame *= recoil

Edit: I also tried CFrame.Angles(math.rad(0), – More math.rad(0)'s

Do this:

By setting reverseGoal to CFrame.new(), the recoil will be reset to the initial position, effectively reversing it.

goal:lerp(recoil)‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

I believe you can use “-” to reverse numbers

maybe this’ll work

local recoil = CFrame.new()
		Recoiling = true
		
		local con; con = game["Run Service"].RenderStepped:Connect(function()
			local randomX = math.random(1.5, 3)
			local randomY = ((math.random(1, 2) == 1) and 1.5) or -1.5
			
			local goal = CFrame.Angles(math.rad(randomX), math.rad(randomY), 0)
			
			recoil = recoil:Lerp(goal, 0.05)
			Camera.CFrame *= recoil
			
			task.wait(0.1)
			recoil = recoil:Lerp(CFrame.Angles(math.rad(-randomX), math.rad(-randomY), 0), 0.05) -- Attempt to reverse
			Camera.CFrame *= recoil
			
			task.wait(0.1)
			if (con) then
				Recoiling = false
				con:Disconnect();
			end
		end)

can you give me an example on how i’d do this?

Already tried that, it will reverse a little bit but not to it’s original state

You can do this likewise here

local recoil = CFrame.new()
local Recoiling = false

local function applyRecoil()
	Recoiling = true
	
	local con; con = game["Run Service"].RenderStepped:Connect(function()
		-- Generate random recoil values
		local randomX = math.random(1.5, 3)
		local randomY = ((math.random(1, 2) == 1) and 1.5) or -1.5
		
		-- Define the goal rotation for recoil
		local goal = CFrame.Angles(math.rad(randomX), math.rad(randomY), 0)
		
		-- Apply recoil
		recoil = recoil:Lerp(goal, 0.05)
		Camera.CFrame *= recoil
		
		task.wait(0.1)
		
		-- Reverse recoil by interpolating back to the original CFrame
		local reverseGoal = CFrame.new()
		recoil = recoil:Lerp(reverseGoal, 0.05)
		Camera.CFrame *= recoil
		
		task.wait(0.1)
		
		if (recoil == reverseGoal) then
			Recoiling = false
			con:Disconnect()
		end
	end)
end

applyRecoil()

This will not work. Lerping it to CFrame.new() won’t make a difference, at most it’ll just stop the recoil

multiplying Camera.CFrame with goal:Inverse(), you can reverse the recoil