Linear Interpolation problem

local dist = (PositionA - PositionB).Magnitude
local Speed = script.Parent.Speed.Value
local center = PositionA:lerp(PositionB, Speed/dist)

I’m using this formula for interpolating a part to another part, (down a runway to be more precise)
I see the issue, it uses Magnitude and it slows the part down as it gets closer
Does anyone know how to fix this? I have no clue what would be a better formula here.

To be more concise, it works, but inefficiently, and isn’t guaranteed to be linear due to the magnitude constantly shrinking.

Hey! I’m not that known with linear interpolation but couldn’t you just have a fixed number? Like local Speed = 0.1?

1 Like

Yeah, though I duplicated this script across all of the parts I’m interpolating. I just found it easier to edit a value outside of the script. Thanks for the suggestion.

It seems like you’re updating the values each step of the loop, if you want it to be linear, you’d only want the initial distance, not an updated one. Try placing the initial distance outside of your loop that way it’s not being updated every iteration.

2 Likes

You could use a for loop or TagService for that, instead of duplicating them in every part.

Like this:

local speed = 0.1

for i,part1 in pairs(workspace.Parts:GetChildren()) do
   spawn(function()
      for _,v in pairs(workspace.Runway:GetChildren() do
           local center = part1:lerp(v, Speed)
      end
   end)
end

If you are talking about my code, I copied that part of the source code in real quick but yeah, that would be smarter to do. I’ve changed it.

1 Like