Making a part move from one point to an other in constant speed (No tweenservice)

I can’t use tweenservice since I need to know exactly when the tween updates however tweenservice has no callback for this.

local Destiny = workspace.Baseplate.Position

local Origin = workspace.TestPart.Position

local SchedularService = game:GetService('RunService')

for i = 0,1,0.001 do

workspace.TestPart.Position = Origin:Lerp(Destiny, i)

SchedularService.Heartbeat:Wait()

end

Now this works, but there’s a problem it doesn’t take into account velocity, this is the problem with using lerp. Is there any way to imitate the linear constant movement done with tweenservice. And if there is an other way I would glad to hear, but also I would like to hear how tweenservice does it.

I have seen some modules implement tween like this

local function tween(duration, callback)
    local startTim = os.clock()
    local tweenCon, tweenCon = RunService.Heartbeat:Connect(function()
        local elapsed = os.clock() - startTim
        local delta = elapsed / duration

        if (delta >= 1) then 
            delta = 1
            tweenCon:Disconnect()
        end
        callback(delta)
    end)
end

local dist = (origin - goal).Magnitude
local velocity = 50 --studs / s
local estimatedDuration = dist / velocity

tween(estimatedDuration, function(delta)
    part.Position = origin:Lerp(goal, delta)
end)

Edit2: After i finished typing and looking at it now all i needed to say was that duration of tween is pre-calculatable by distance divided by velocity lol

This actually is a pretty good solution. One thing I’m concerned about though is, is the velocity actually accurate or is it slightly off?

Also, is this the way roblox’s tweenservice does it (just asking for curiosity)

the velocity should mathmatically be correct, representing the constant velocity. (the formula you should have learn around elementary or secondary, which is: speed = dist / time, time = dist / speed, , dist = speed * time)

I am not sure of how tween service works, but its definitely an interesting topic to research about in the future XD

1 Like