Make rotation complete midway through a lerp

I’m relatively new to using the :Lerp() function or linear interpolation function, and I wanted to ask for some help about them. I have changed the movement in my Tower Defense game to lerps instead of humanoid movement due to humanoid movement being pretty unreliable. However, at my current knowledge of lerps, I don’t know how to make a rotation complete midway through a lerp, and not at the end or have it rotate throughout the entire lerp. Below I’ve made a rough video showing a graphic of what I’m looking for. Is there any way I can do the last example?

1 Like

Not sure what I can do without a code example, but I assume you’re looking for something like this:

local RunService = game:GetService("RunService")

local interpolation_speed = .5
local some_part = workspace.Part
-- test nodes
local nodes =
{
    CFrame.new(0, 0, 0);
    CFrame.new(0, 0, -10);
    CFrame.new(-10, 0, -10) * CFrame.Angles(0, math.rad(90), 0);
}

local alpha = 0
for index = 1, #nodes - 1 do
    local start = nodes[index]
    local goal = nodes[index + 1]

    while alpha < 1 do
        local position_lerp = start.Position:Lerp(goal.Position, alpha)
        local angle_lerp = start.Rotation:Lerp(goal.Rotation, math.min(alpha / .1, 1))

        some_part:PivotTo(CFrame.new(position_lerp) * angle_lerp)
        alpha += RunService.Heartbeat:Wait() * interpolation_speed
    end
    alpha %= 1
end

Result:

What this script does is create a table of nodes (you can easily substitute that table by just getting the pivots of your tower defense game’s nodes), and then interpolates the position and angle separately. By doing math.min(alpha / .1, 1), it’s making sure that the angle is set to the next node’s angle 10x faster (can be written as math.min(alpha * 10, 1)). Finally, the part is pivoted to the position of the interpolation multiplied by the prematurely completed angle. I hope this helps.

4 Likes

I’ll give this a try. If I can’t get this working, I’ll provide the code I’m using.

1 Like

This ended up working pretty well, I was able to insert part of this code into my current lerp function and got it to work fine. Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.