Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't lerp go to correct spot?

Asked by 9 years ago

I am making a module to lerp a brick's position to a new one. For some reason it goes past the spot, never actually hitting the correct position. I can't solve the problem, probably something with my math, or setup, but other than that it should be fine. Thanks in advance!

local module = {}

module.lerpBrickPosition = function(brick, position, lerpTime, bodyPos, stepAmount)
    --[[
        "brick" ........ The brick to lerp
        "position" ..... The position to move it to
        "lerpTime" ..... The total time to move it
        "stepAmount" ... (OPTIONAL) The amount of steps to make to lerp
    --]]

    local xDistance = -(brick.Position.X - position.X)
    local yDistance = -(brick.Position.Y - position.Y)
    local zDistance = -(brick.Position.Z - position.Z)

    local distance = dLAD(brick.Position, position)

    local steps = stepAmount or (100)

    repeat
        local newPos = Vector3.new(xDistance/steps + brick.Position.X, yDistance/steps + brick.Position.y, zDistance/steps + brick.Position.z)
        brick.Position = newPos
        if bodyPos ~= nil then
            bodyPos.position = newPos
        end
        wait(lerpTime/steps)
    until brick.Position == position
end

function dLAD(position, position2) -- Returns the largest axis difference (Determine Largest Axis Difference)
    local x = math.abs(position.X - position2.X)
    local y = math.abs(position.Y - position2.Y)
    local z = math.abs(position.Z - position2.Z)
    if x >= y and x >= z then
        return x
    elseif y >= z then
        return y
    else
        return x
    end
end

return module

Answer this question