How to make a part approach the player at a constant speed

The problem with using lerp or tween for this is that you’re effectively going about the equation backwards, even though they technically work. How a lerp works is that it returns a value between value A and B, the value being determined by the second argument. 0 or 1 return the same value as A or B respectively, and any other number is (x/100)% of B starting from A. This post explains it a little better.

Tween is almost the same thing, except it creates a loop bound to framerate that starts at 0 and then ends at 1 across the time given to complete the tween.

Instead, since your concern is speed rather than percentage, you’ll want to use cframes to direct and position the part, like this.

LoopSpeed = 0.05
TravelSpeed = 5 --//Studs per second.
while true do
	--//First part of CFrame.new specifies position, and the second argument specifies where it is "looking".
	--//Then it is multiplied (how cframes are combined) by the speed to move it forwards.
	CloserDirection = CFrame.new(MovePart.Position,Target.Position) * CFrame.new(0,0,-TravelSpeed /LoopSpeed )

	--//This keeps rotation
	MovePart.Position = CloserDirection.Position

	--//Alternatively, this makes the part face the direction it's moving.
	MovePart.CFrame = CloserDirection
	task.wait(LoopSpeed)
end

You’ll probably also want a magnitude check to make the part teleport to the target directly instead when it’s close enough, otherwise it’d overshoot very slightly.

2 Likes