How to make entity lerping smoother?

So I have a rooms game, and I make it so the entities lerp from start to finish. Usually it’s choppy, and the hitbox overshoots players. How can I fix this?

local RunService = game:GetService("RunService")
local part = script.Parent.MainPart

local alpha = 0
local speed = 50
local startCFrame = script.Parent.PrimaryPart.CFrame

RunService.Heartbeat:Connect(function(delta)
	if script.Parent.Temp.Value == false then
		local lastRoom = game.Workspace.Rooms[game.ReplicatedStorage.CurrentRoom.Value].Exit
		local distance = (script.Parent.PrimaryPart.Position - lastRoom.Position).Magnitude
		local relativeSpeed = distance / speed
		local goalCFrame = startCFrame:Lerp(lastRoom.CFrame, alpha)
		script.Parent:PivotTo(goalCFrame)
		alpha += delta / relativeSpeed
		if script.Parent.MainPart.Position.X < lastRoom.Position.X then
			script.Parent:PivotTo(lastRoom.CFrame)
			script.Parent:Destroy()
		end
	end
end)
1 Like

Are you lerping with the intention of smoothly changing the position of the entities? I feel like TweenService is a way better method of doing something like this.

2 Likes

But the farther you go the faster the entities will be, because it’s a value in seconds, not in speed.
Unless I’m not understand TweenService correctly.

1 Like

Tweenservice is essentially just used to smoothly interpolate using various different forms (linear,sine,exponential,etc).

You are right about this, but you can quickly solve this issue using basic kinematics, you are already defining what the speed should be speed = 50, and you also are able to get the distance from this line:

and thus, we can know what value in seconds TweenInfo.new(time) should have using this equation:

time = distance/speed
2 Likes

Thanks!

(character limit asdadadadasdasd)

2 Likes

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