Lerp not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Attempting to lerp a sprint animation.

  2. What is the issue? Include screenshots / videos if possible!

Without lerping and just using CFrame:PivotTo

With lerping

No matter what I set the value to it goes into the same spot shown in this video.

		elseif input.KeyCode == Enum.KeyCode.LeftShift then
			if humanoid then
				if humanoid.Health > 0 then			
					humanoid.WalkSpeed = 20
					sprinting = true		
									
					local targetCFrame = viewmodel.PrimaryPart.CFrame * CFrame.new(-.4, -4.7, -0.35)
					for i = 0, 1, 0.01 do
						task.wait()
						viewmodel.PrimaryPart.CFrame:Lerp(targetCFrame, i)
					end		

I however do have this running through a heartbeat function too…

					viewmodel:WaitForChild("RootPart"):PivotTo(
				
				-- sway
				hrp.CFrame * CFrame.new(movementsway.Position.X / 2, movementsway.Position.Y / 2, 0)
				* CFrame.Angles(0, -mousesway.Position.X, mousesway.Position.Y)
				* CFrame.Angles(0,movementsway.Position.Y * sprintswaymultiplier, movementsway.Position.X * sprintswaymultiplier)
				
				-- recoil
				* CFrame.Angles(gunspring.Position.X, gunspring.Position.Y, 0)
				* CFrame.new(0, 0, gunspring.Position.X * 5)
				-- + Vector3.new(-.4, -4.7, -0.35) -- this is how the movement was handled prior to lerping
			)

that controls the recoil + cam and weapon sway when walking. As you can see the first video adds the value as a vector3, and the second video multiplies it as a CFrame. (I think this might have something to do with it?)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Can’t find a solution
3 Likes

Does adding a Vector3 to a CFrame work? I never even tried doing that.

Your first section of code that has the Lerp is likely just being overwritten. The heartbeat version works, because that is where you should be updating the CFrame (within Heartbeat)
Task Scheduler | Documentation - Roblox Creator Hub

1 Like

You mean like this?

			--when sprinting
				
				
			local targetCFrame = viewmodel.PrimaryPart.CFrame + Vector3.new(-.4, -4.7, -0.35)
			local lerp = viewmodel.PrimaryPart.CFrame:Lerp(targetCFrame, 0.05)
			
			viewmodel:WaitForChild("RootPart"):PivotTo(
				
				-- sway
				hrp.CFrame * CFrame.new(movementsway.Position.X / 2, movementsway.Position.Y / 2, 0)
				* CFrame.Angles(0, -mousesway.Position.X, mousesway.Position.Y)
				* CFrame.Angles(0,movementsway.Position.Y * sprintswaymultiplier, movementsway.Position.X * sprintswaymultiplier)
				
				-- recoil
				* CFrame.Angles(gunspring.Position.X, gunspring.Position.Y, 0)
				* CFrame.new(0, 0, gunspring.Position.X * 5)
				* lerp
				--+ Vector3.new(-.4, -4.7, -0.35)
			)

This is what happens now…

I feel like I’m doing something really stupid haha

1 Like

Well, if I I’m understanding correctly. I guess adding a vector3 is just giving an offset to the position of the CFrame? I assume CFrame * CFrame.new(Vector3) is the equivalent to CFrame + Vector3?
Lol now I’m feeling stupid. :disappointed_relieved: I’ve always just multiplied a default oriented CFrame.

So perhaps in the hearbeat method, do the pivot calculations and then perform the lerp to the resulting Cframe

--when sprinting
				
				
			local targetCFrame = viewmodel.PrimaryPart.CFrame + Vector3.new(-.4, -4.7, -0.35)
			local lerp = viewmodel.PrimaryPart.CFrame:Lerp(targetCFrame, 0.05)
			
			viewmodel:WaitForChild("RootPart"):PivotTo(
				
				-- sway
				hrp.CFrame * CFrame.new(movementsway.Position.X / 2, movementsway.Position.Y / 2, 0)
				* CFrame.Angles(0, -mousesway.Position.X, mousesway.Position.Y)
				* CFrame.Angles(0,movementsway.Position.Y * sprintswaymultiplier, movementsway.Position.X * sprintswaymultiplier)
				
				-- recoil
				* CFrame.Angles(gunspring.Position.X, gunspring.Position.Y, 0)
				* CFrame.new(0, 0, gunspring.Position.X * 5)
				--+ Vector3.new(-.4, -4.7, -0.35)
			)
			viewmodel.RootPart:PivotTo(viewmodel.RootPart:GetPivot():Lerp(targetCFrame, 0.05))
1 Like

Don’t worry, this is making me feel very stupid too hahaha.

I mean it works better than it did?

1 Like

Wait, I just realised that the targetcframe would be updated to a new amount further down every heartbeat…

1 Like

Well now I just need to understand the goal. The V3 being added was just giving the CFrame of the viewModel root part an offset. Your changing that to lerping toward a targetCFrame. I’m not sure those things are accomplishing the same goal.

1 Like

It’s for a sprint animation, so when I press shift it lerps my hands down into that position.

1 Like

Try this:

--when sprinting
				
				
			--local targetCFrame = viewmodel.PrimaryPart.CFrame + Vector3.new(-.4, -4.7, -0.35)
			--local lerp = viewmodel.PrimaryPart.CFrame:Lerp(targetCFrame, 0.05)
			
			viewmodel:WaitForChild("RootPart"):PivotTo(
				
				-- sway
				hrp.CFrame * CFrame.new(movementsway.Position.X / 2, movementsway.Position.Y / 2, 0)
				* CFrame.Angles(0, -mousesway.Position.X, mousesway.Position.Y)
				* CFrame.Angles(0,movementsway.Position.Y * sprintswaymultiplier, movementsway.Position.X * sprintswaymultiplier)
				
				-- recoil
				* CFrame.Angles(gunspring.Position.X, gunspring.Position.Y, 0)
				* CFrame.new(0, 0, gunspring.Position.X * 5)
				--+ Vector3.new(-.4, -4.7, -0.35)
			)
			--Calculate the targetCFrame now, based off the newly calcluated CFrame
			targetCFrame = viewmodel.PrimaryPart.CFrame + Vector3.new(-.4, -4.7, -0.35)
			--Then do the lerp to that targetCFrame
			viewmodel.RootPart:PivotTo(viewmodel.RootPart:GetPivot():Lerp(targetCFrame, 0.05))
1 Like

Sorry for the late response, but that just puts the hands above the head again and they stay there.

1 Like

When lerping in an active context, something that is being updated every frame, you are not going to want to use the approach where you have a separate thread iterating through a full alpha over the course of roughly 100 frames, as done here

local targetCFrame = viewmodel.PrimaryPart.CFrame * CFrame.new(-.4, -4.7, -0.35)
for i = 0, 1, 0.01 do
	task.wait()
	viewmodel.PrimaryPart.CFrame:Lerp(targetCFrame, i)
end

What you’re essentially doing there is a fixed tween, which won’t work well with all of the conflicting operations being done on the same objects and values regularly. Instead you’re going to want to only update a targetCFrame, and then use the heartbeat to lerp inbetween the current and target cframe

EDIT:
But rather than reinventing the wheel, you may want to look into blending roblox animations using adjustWeight on animation tracks

1 Like

Are the “PrimaryPart” and “RootPart” the same part?

I agree, this would be done via animation for me.

That’s true actually, I might just do that. I just completely wrote off the option of personally making any animations but this would be very simple to do. Thank you guys, I don’t know why I didn’t think to do that…

1 Like

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