Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to control the Speed of a lerp?

Let's say I wanted my lerp to last exactly 2 seconds. How would I do that?

1 comment

0

Try using some maths like multiplying, deviding, I've also been tried to make something like this before but never worked.

2 answers

-- Declaration Section
local MaxTime = 2 --Change Value
local Increment = 0.25 --Change Value
for number = 0, MaxTime, Increment do
	--Your lerp function goes here along with the number. Here's an ex.
	Camera.CFrame = Part1.CFrame:lerp(Part2.CFrame, number)
end 

5 comments

0

what is the purpose of Increment? Does it effect the speed?

0

^ Yup, increment is basically the speed

0

This isn't how this works.

0

Mind explaining Avigant?

0

This code wouldn't work, you need to use an easing function. Check my answer for a hint in the right direction, you can apply it to what you're doing. TweenService is also an option.

We need to use an easing function to do this. Here's a collection of useful easing functions for your reference: https://github.com/EmmanuelOga/easing/blob/master/lib/easing.lua

--[[
Disclaimer for Robert Penner's Easing Equations license:
TERMS OF USE - EASING EQUATIONS
Open source under the BSD License.
Copyright © 2001 Robert Penner
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]

-- For all easing functions:
-- t = elapsed time
-- b = begin
-- c = change == ending - beginning
-- d = duration (total time)

local function linear(t, b, c, d)
  return c * t / d + b
end

TweenService can abstract this for you should you choose to use it.

Worth noting that Roblox's CFrame:lerp() actually performs slerp (spherical linear interpolation).

7 comments

0

https://www.youtube.com/watch?v=ft7t8xHbehM. I used my above code to strictly accomplish this.

0

So this will work like lerp, or tween?

0

I don't see what you mean. Your answer wasn't correct, if that's what you're implying.

0

You could simply use this easing function or TweenService, either way you'd get the same functionality. TweenService just abstracts the act of easing for you so you don't need to worry about it.

0

I want to know why it wont work because I have been doing using this way for a while and If it is not the way to do so, I would like to change it.

0

Alpha is a number between 0 and 1, you're not waiting any amount of time between calls, and towards the end of your interpolation the distance covered would be smaller and smaller. You need to ease it.

0

The only problem with Tween is that it doesn't rotate...

This discussion is preserved as it was. New answers, comments, and votes are closed.