CFrame lerp isn't working

I want to make fps arms go to the camera smoothly

RUN_SERVICE.Heartbeat:Connect(function()
	local position = CAM.CFrame * CFrame.new(0, -2, 0) * CFrame.Angles(0, math.rad(90), 0)
	--local CamPos = Vector3.new(CamFrame.x, CamFrame.y, CamFrame.z)
	
	for i = 0, 1, .1 do
		FPS_ARMS.HumanoidRootPart.CFrame:Lerp(position, i)
		print(i)
		wait(.1)
	end
end)

Its just not doing anything though so I don’t know what to do. Its not giving me any errors or anything

I’ve been putting prints in places and they PRINT but its not lerping

2 Likes

In your for loop try putting the CFrame into a variable and assigning the arm CFrame to it.

Local CFrameMath = FPS_ARMS.HumanoidRootPart.CFrame:lerp(position, i)
FPS_ARMS.HumanoidRootPart.CFrame = CFrameMath
4 Likes

A few notes that should help: Heartbeat fires many times per second. The for loop in your code calls wait(.1), so you potentially have many loops running at once. This isn’t a really good pattern to use here.

Rather, you probably want to record the start time of the animation, then every frame determine how long the animation has been playing for. Using that value, determine alpha (the 0-1 value for the tween), and use your start/goal values in the call to Lerp.

local DURATION = 1 -- in seconds
-- From where to where?
local startCF = FPS_ARMS.HumanoidRootPart.CFrame
local goalCF = CAM.CFrame * CFrame.new(0, -2, 0) * CFrame.Angles(0, math.rad(90), 0)

local startTime = workspace.DistributedGameTime
RUN_SERVICE.Stepped:Connect(function ()
    local deltaTime = workpsace.DistributedGameTime - startTime
    local alpha = math.clamp(deltaTime / DURATION, 0, 1)
    FPS_ARMS.HumanoidRootPart.CFrame:Lerp(goalCF, alpha)
end)

Hope this points you in the right direction!

4 Likes

I did kind of what you said but it still does nothing.

RUN_SERVICE.Heartbeat:Connect(function()
	local goal = CAM.CFrame * CFrame.new(0, -2, 0) * CFrame.Angles(0, math.rad(90), 0)
	local deltaTime = workspace.DistributedGameTime - START_TIME
	local alpha = math.clamp(deltaTime/1, 0, 1)
	
	print(deltaTime)
	
	FPS_ARMS.HumanoidRootPart.CFrame:Lerp( goal, alpha )
end)

But it doesn’t move to the fps arms to the camera CFrame. I don’t know if I missed anything or anything, It looks and sounds like it should work and makes sense but it doesn’t give any errors in the console or anything. Did I do something wrong? Sometimes I miss stuff super obvious. I’m just really confused why it isn’t doing anything, not even an error message. (Note: this is in a client script)

I don’t know if you’ve done something wrong because I don’t see all of your code. Perhaps you should verify that the script is running in the first place with some kind of call to print after you connect to Heartbeat

1 Like

alpha is 1 constantly do you know why?

local START_TIME = workspace.DistributedGameTime

local startCF = FPS_ARMS.HumanoidRootPart.CFrame

RUN_SERVICE.Heartbeat:Connect(function()
	local goal = CAM.CFrame * CFrame.new(0, -2, 0) * CFrame.Angles(0, math.rad(90), 0)
	local deltaTime = workspace.DistributedGameTime - START_TIME
	local alpha = math.clamp(deltaTime/1, 0, 1)
	
	print(alpha)
	
	FPS_ARMS.HumanoidRootPart.CFrame = startCF:Lerp(goal, alpha)
end)

Well, that would be because deltaTime is greater than 1. The division deltaTime/n (in your code, deltaTime/1) determines how long the tween should go for.

I don’t think you understand what the code @Ozzypig wrote actually does.

This code will perform a lerp for the first second of gameplay, and then it will stay at the goal position for the remainder.

If I were you, and this is just a cosmetic effect, I would just use this dead simple option, properly time-stamped interpolating be darned.

local approachPercent = 0.01 -- percentage to move per frame, might need to increase this a lot idk

game:GetService("RunService").Stepped:Connect(function()
	local goal = CAM.CFrame * CFrame.new(0, -2, 0) * CFrame.Angles(0, math.rad(90), 0)

	FPS_ARMS.HumanoidRootPart.CFrame = FPS_ARMS.HumanoidRootPart.CFrame:Lerp(goal, approachPercent)
end)
1 Like