How would I move a model from point A to point B using a set speed?

Hello there. I will try to explain this as briefly as possible. I have a part and I want it to move from Point A to Point B. Now, I would use TweenService, but the issue with TweenService is that it uses time instead of speed. Say I wanted to move at a speed of 2 studs / second. If I use TweenService, I will have to specify a time such as 10 for a distance of 20 studs. Although this will work, if the distance changes, the time would also have to change. Additionally, if I want to speed up or slow down the part while in motion, I will have to be canceling the Tween and recalculating. This just does not seem like a proper way to implement this system.

What I need is some way to have a set speed and have the part move from Point A to Point B smoothly (like a Tween would) while having the ability to change the speed of said object while in motion. Lastly, I am planning to have the brick anchored, so the BodyPosition modifier won’t really work (unless it has a workaroudn for anchored parts). The reason for all of this fuss is because I want something similar to a train system, so it will be moving through a set of points. Having that in mind, I want to be able to change the speed of the “train” efficiently while it is moving throughout the track (which will be multiple points laid out). If someone could assist me with this or point me in the right direction, that would be awesome. Thank you for your time and help, I appreciate it.

If the part is anchored, you can only really change the position of it with decent results with CFraming (aka TweenService). Otherwise you will have to unanchor it and use a BodyMover such as BodyVelocity or BodyPosition.

Although I would love to use BodyMovers, I do not trust Roblox’s physics engine that a cart won’t just fly off because of a glitch. When it is anchored though, the chances of that happening are zero since there are no physics involved. Thank you for the reply though.

Yea the only way (that I know of) to get around that is to not have your object(s) cancollide, so the physics wont fling the part with a weird collision.

Yep, tweens are pretty weird if you have to cancel and play them continuosly every frame.

Consider constant speed lerping, I got the formula from here not sure how it works.

local part = script.Parent
local part2 = Workspace.Part2

while true do
	local dt = task.wait()
	local distance = (part2.Position - part.Position).Magnitude
	local speed = 10*dt -- distance traveled between elapsed time
	local estimatedTime = speed /distance -- obtain a lerp fraction between distance traveled in a frame divided by the overall distance towards the goal
	local adjustedLerpAlpha = math.min(estimatedTime,1) -- prevent the lerp from going over 1 which is over the lerp goal

	part.CFrame = part.CFrame:Lerp(part2.CFrame,adjustedLerpAlpha) -- lerps the position values at constant speed
end
16 Likes

@Batimius you should set this as solution

2 Likes

This worked P E R F E C T L Y. With a few more calculations, I was able to get a track system going, making it follow a certain path dynamically. Thank you so much for the help!

2 Likes