CFrame:lerp How to use it properly

So, to clarify, you only want to move the part along the y axis? up and down?

I don’t think lerp is the right use case for such a task.

1 Like

Change that

water.CFrame.Y = water.CFrame:lerp(CFrame.new(water.CFrame.Y, stop.CFrame.Y),i)

to that

water.CFrame = water.CFrame:Lerp(stop.CFrame, i/timelimit)

Because i must be between 0 and 1 inclusive.

1 Like

opps forgot to add that in, thanks

Yes, I only want the part to move up and down, although it is not a must, as I figured out a way, I simply made an exact copy of water, named it stop, and moved it on the Y axis, that way it will line up properly.

1 Like

It worked! Thanks!
Only problem is that it moves really blocky like.
I added a video so you know what I mean

It almost looks as if I was setting the CFrame to the same value but 4 studs above the original one very second. I think that is pretty much what is happening, but I wanted to use lerp so it went up smoothly. This is caused by the wait(1) but if I remove it, it just teleports to the top.
I used to use BodyVelocity, but the water just kept going up and I couldn’t really control the time it took to reach the top, so I switched to lerp.
Is there a better way to do this?

Yes, you can increase the amount of iterations

local iterations = 10
for i = 0, timelimit - 1 do
    for j = 0, iterations do
        water.CFrame = water.CFrame:Lerp(stop.CFrame, (i * iterations + j)/(timelimit * iterations))
        wait(1/iterations)
    end
end
1 Like

1 would be 100% in the Lerp function.

Instead of doing this:

for i = 0, timelimit, 1 do -- Time limit is incrementing by 1, or 100%

try using this:

for i = 0, 1, 1/timelimit do -- Time limit is incrementing by 1/70 or ~1.43%
1 Like

What are iterations? Is it like the ammount of time that code runs?
And what are all the multiplications for?
I want to understand the code so I can replicate it if I encounted the same problem.
Also, the code didn’t quite work, it teleported the water to the stop position.
But thanks anyways, I want to learn how this whole thing works so it would be great if you could explain the code aswell.

2 Likes

Yes, that does work!
But there are two problems.
1-. Water rises extremley slowly
2-. Water still rises in blocks, which ruins the whole effect.
Here is a vid of what I mean


I want the water to reach the top in the time-limit(70 seconds), and I wanted to use lerp so that it goes up seamlessly, without it being blocky.

1 Like

Then why not use TweenService?

TweenService guarantees smooth transitions.

For instance:

local TS = game:GetService("TweenService")
local Ti = TweenInfo.new(70)

Ts:Create(part to tween, TI, {Position = Desired position}):Play()
16 Likes

Oops, a mistake, try this

local startCFrame = water.CFrame
local iterations = 10
for i = 0, timelimit - 1 do
    for j = 0, iterations do
        water.CFrame = startCFrame:Lerp(stop.CFrame, (i * iterations + j)/(timelimit * iterations))
        wait(1/iterations)
    end
end
3 Likes

This is how it looked when I used BodyVelocity, this is what I mean by smooth transitions.
Thing is I don’t want to use BodyVelocity as I cannot control the time it takes for the water to reach point A to point B and also the water doesn’t stop at point B.

My guess is that your best select is tween service.

2 Likes

How could I use TweenService? I did open a wiki article on it a while back for scaling gui using Tween but I just thought it was for GUI and lerp was for objects.

I’ll do more research on Tween, but that sitll leaves me the queston on what lerp is for? I thought lerp was also for smooth transitions.

Lerp is for saving pain on calculations, and can be useful. I’ve edited my post above, hope it helps.

2 Likes
local water = script.Parent.Water
local stop = script.Parent.Stop
local timelimit = 70

local tweenservice = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(timelimit,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
local goal = {position = stop.Position}
local tween = tweenservice:Create(water,tweeninfo,goal)
wait(5)
tween:Play()

5 Likes

I hope this helps if not let me know! Also it might got spelling mistake as I didn’t test it. So check that.

1 Like

It did work thank you very much but I think I’ll go for Tween.
Thank you though!

Yup, it worked!
Just had to fix some caps in Ts and Ti.
Thank you very much @VegetationBush and @Mr_Gamerious for the help on tweening!

7 Likes

I would recommend using run service for that and divide deltaTime by the expectedTime variable, before adding(+=) it to alpha value, to await the function to be finished set a bool value and change it, after the alphaValue reaches your set value by doing that

CanPass = alphaValue >= SetMaxValue

And then disconnect the event.