:Lerp() function super fast on high FPS


--code snippet
local recoilcf = cfnew()
local camrecoilcf = cfnew()

local to_recoilcf = cfnew()
local to_camrecoilcf = cfnew()

run.RenderStepped:Connect(function(dt)
     recoilcf = recoilcf:Lerp(to_recoilcf, .4)
     camrecoilcf = camrecoilcf:Lerp(to_camrecoilcf, .4)

     camera.CFrame *= camrecoilcf
     view_model.Root.CFrame *= recoilcf

     to_recoilcf = to_recoilcf:Lerp(CFrame.new(), .1)
     to_camrecoilcf = to_camrecoilcf:Lerp(CFrame.new(), .2)
end)

user.InputBegan:Connect(function(ss)
  if ss.UserInputType == Enum.UserInputType.MouseButton1 then
      to_recoilcf = to_recoilcf* CFrame.new(0,0, pushback) * CFrame.Angles(pitch, yaw, roll)
      to_camrecoilcf = to_camrecoilcf*CFrame.new(0,0, pushback/3) * CFrame.Angles(pitch/4.5, yaw/3, roll/3)
  end
end)

im aware that it lerps faster because of the higher framerate obviously, but even after attempting to multiply it with the deltatime, or deltatime*60, it just dont budge (it overshoots)

im not sure why, i have other lerp functions in the same loop which do just fine when multiplying them with deltatime*60, although those are custom lerp functions for numbers only: number + (target-number) * speed

glad if someone could help out!

3 Likes

I have a mechanic in my game that locks projectiles to 60 fps, here is that code:

(Delta being deltaTime)

local framerate = 1/delta
local ns = (framerate/60)
local speed = if framerate > 60 then 4.5/ns else 4.5

4.5 in this case is the speed. Idk if this will help but rn i dont have the time to help you super in-detail

3 Likes

understandable thanks for the reply ill have a go at trying this tomorrow

1 Like

bump still need helkp with thisaaaaaa
a

I provided a solution that worked for my game. I can elaborate if needed

1 Like

Thanks for the reply. Yes, please elaborate. Iā€™m not sure how I would implement your, what I assume is a rate limited bullet velocity, into my :Lerp() function.

Fundamentally, what my code does is lower a number to make everything run at 60 fps or lower.

you can multiply this to the 2nd number in your lerp function to make the lerp have less effect when the framerate is over 60.

delta = (1/120)

local framerate = 1/delta
local ns = (framerate/60)
local speed = math.min(1, 1/ns)

print(speed) -- 0.5

As you can see in the code above, since the delta is 2x the speed of 60 fps, speed will be 0.5.

You could apply this in your lerp code as follows (untested, needs to be played around with)

--code snippet
local recoilcf = cfnew()
local camrecoilcf = cfnew()

local to_recoilcf = cfnew()
local to_camrecoilcf = cfnew()

run.RenderStepped:Connect(function(dt)
	local framerate = 1/dt
	local ns = (framerate/60)
	local speed = math.min(1, 1/ns)
	
	recoilcf = recoilcf:Lerp(to_recoilcf, .4 * speed)
	camrecoilcf = camrecoilcf:Lerp(to_camrecoilcf, .4 * speed)

	camera.CFrame *= camrecoilcf
	view_model.Root.CFrame *= recoilcf

	to_recoilcf = to_recoilcf:Lerp(CFrame.new(), .1 * speed)
	to_camrecoilcf = to_camrecoilcf:Lerp(CFrame.new(), .2 * speed)
end)

user.InputBegan:Connect(function(ss)
	if ss.UserInputType == Enum.UserInputType.MouseButton1 then
		to_recoilcf = to_recoilcf* CFrame.new(0,0, pushback) * CFrame.Angles(pitch, yaw, roll)
		to_camrecoilcf = to_camrecoilcf*CFrame.new(0,0, pushback/3) * CFrame.Angles(pitch/4.5, yaw/3, roll/3)
	end
end)
1 Like

I tested your code snippet and it does work albeit only for the viewmodel recoil, the camera still overshoots but honestly it shouldnt. probably an issue on my end ill have to look into that. thanks for the help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.