Tweening at same speed but different directions and lengths

I’m tweening 2 models, and I want them to move at the exact same speed but in a different and changing direction and distance. I have 2 “roads” of waypoints (parts) that represent where I want them to go, and I want them to move at the same time.

Waypoints1= --table of parts that make the path model1 follows
Waypoints2= --same thing, but for model2
local count1= 1
local count2 = 1

local function Move(model)
    local nextplace
    local currentplace = model.Position --simplified for clarity
    local nextcframe
    If model.Name == "Model1" then
        nextplace = Waypoints1[count1].Position
        nextcframe = Waypoints1[count1].CFrame
    else
        --same thing, but for model2, Waypoints2, and count2
    end
    local Speed = 5
    local Distance = math.abs(currentplace.Magnitude - nextplace.Magnitude)
    local time = Distance/Speed
    local tween = TweenService:Create(model,TweenInfo.new(time),{CFrame = nextcframe})
    tween:Play()
    tween.Completed:Connect(function() 
        tween:Destroy()
        count1 += 1
        --if model2 then count2
        Move(model)
    end)
end

Move(Model1)
Move(Model2)

This is what I have so far. One of them is moving much faster than the other. What am I doing wrong?

1 Like

This line is causing your problem:
local Distance = math.abs(currentplace.Magnitude - nextplace.Magnitude)
Instead, it should be:
local Distance = (currentplace - nextplace).Magnitude

This is because you’re subtracting the two “places” distance from 0,0,0. Instead, you want to subtract the two vectors to get a vector that represents the offset between the positions, then find the magnitude of that.

Edit:
I drew something to show what I was having trouble explaining:
213213423
A <1>
B <-1>
C <2>
(C-A) = <1>
(B-A) = <-2>
(B.Mag - A.Mag) = (1 - 1) = 0
(B-A).Mag = 2

See how subtracting the Vector1s (1 dimensional vectors) results in a vector that represents the distance between them? If you took the length of B and subtracted the length of A, the result would be 0, but the distance between them is 2.

4 Likes

Besides the solution provided by @PersonifiedPizza , you could do a this, so both Move functions execute at nearly the same time:

 task.spawn(Move(Model1))
 task.spawn(Move(Model2))
1 Like

DFGotMeV2
Thank you very much. I was worried no one could help me.

4 Likes