CFrame:lerp How to use it properly

What I am trying to achieve is a part(named water) which stays the same in X and Z value but rises in the Y value until it reaches another part(named stop) which you can’t collide and is transparent, basically, a ghost part. I want the part to do this within the timeframe.
Here is my script

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

for i=0,timelimit,1 do
water.CFrame.Y = water.CFrame:lerp(CFrame.new(water.CFrame.Y, stop.CFrame.Y),i)
wait(1)
end

I have tried multiple combinations, sometimes it asks for a vector3, other times for a CFrame, and I just don’t understand how to use CFrame:lerp.
Could someone explain to me how CFrame:lerp works, please?
Thanks in advance!

10 Likes

CFrame CFrame:Lerp ( CFrame goal, number alpha )
Returns a CFrame interpolated between this CFrame and the goal by the fraction alpha

Try:

  `water.CFrame = water.CFrame:lerp(stop.CFrame,i)`
1 Like

It sortof works, but not for what I am looking for.
It does bring the water to the stop’s position, but it also moves its X and Z value. It also just teleports to the location, it doesn’t do it over the 70 seconds that I placed as time limit.

I did read the article on the wiki but I still don’t understand how it works and how can I make it only lerp on the Y position.

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.

2 Likes