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

CFrame tween help making Cframe tween?

Asked by 8 years ago

Hello, So I am learning CFrame. Can someone explain to me how I would 'tween' a brick to move?

I am not asking for a request; but for someone to explain to me how to do it.

Thanks

1 answer

Log in to vote
2
Answered by
Zeloi 40
8 years ago

So i would use some sort of loop, rather it be a while, for, or repeat loop. I will show you some examples

for i = 0, 1, .05, do
    cframe = lCFrame:lerp(nCFrame, i)
    game:GetService('RunService').Heartbeat:wait()
end


local amountOfTime = 5
local start = tick()
repeat
    cframe = lCFrame:lerp(nCFrame, (tick() - start)/amountOfTime)
    game:GetService('RunService').Heartbeat:wait()
until (tick() - start) >= amountOfTime

As you can see, over time our number increases and ends up at 1, and usually, no more. In the for loop we have the variable, i, increase by .05 every time through the loop, and it ends at 1.

As for the repeat loop, in this example it doesn't end up at precisely 1 everytime. Because the Heartbeat event does not fire every n number of seconds and it is not divisible by 1 all the time. Although, we could end it at 1 by checking if it is already more than one.

local amountOfTime = 5
local start = tick()
repeat
    local alpha = (tick() - start)/amountOfTime
    if alpha <= 1 then
        cframe = lCFrame:lerp(nCFrame, alpha)
    else
        cframe = lCFrame:lerp(nCFrame, 1)
        break
    end
    game:GetService('RunService').Heartbeat:wait()
until (tick() - start) >= amountOfTime

I hope this helps you!

Ad

Answer this question