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

How would I implement sine into my lerp function?

I made some lerp code to move some joints from one position to another. However, I was wondering if I could use a sine style instead of linear.

Here's my lerp function:

function LerpC0(Joint, EndPos, Speed, Force)
	local Code = MR(-1234567890, 1234567890)
	local LCode = Joint:FindFirstChild("LerpCodeC0") or IN("NumberValue", Joint)
	LCode.Name = "LerpCodeC0"
	LCode.Value = Code
	local BaseC0 = Joint.C0
	delay(0, function()
		for i = 0, 1, 1/60/Speed do
			if LCode.Value ~= Code then break end
			Joint.C0 = BaseC0:lerp(EndPos, i)
			HB:wait()
		end
		if LCode.Value == Code or Force then Joint.C0 = EndPos end
	end)
end

I know I would use math.sin(), but how would I use it?

3 comments

0

What easing style do you want? In, Out, or InOut?

0

You could try checking out this place, https://www.roblox.com/games/557635264/Part-Easing-Showcase In it, I incorporate multiple easing functions.

0

Out

1 answer

✓ Accepted answer

In order to understand how to do it, you need to understand what the sin graph looks like.

We know for the Out EasingDirection, It slows down as it approaches the end. When you look at the sin graph, you notice that the rate of change decreases between 0 and 90 degrees (0 and pi/2 radians). When we put this into a function, we get:

local OutEasingDirection = function(Alpha)
	return math.sin((math.pi/2)*Alpha);
end

[Alpha is a value between 0 and 1, relating to the distance along the lerp you are]

Using the same idea, we know that the In EasingDirection can be found using:

local InEasingDirection = function(Alpha)
	return math.sin((math.pi/2)-((math.pi/2)*Alpha));
end

For the InOut direction, we need to go a bit further due to the rate reversing direction and going backwards again. To solve this, we can use the following function:

local InOutEasingDirection = function(Alpha)
	return (1-math.cos(math.pi*Alpha))/2;
end

To incorporate this into your lerp:

for Alpha=1,20 do
	local NewCFrame = StartCFrame:lerp(EndCFrame, InEasingDirection(Alpha/20));
end

Sorry if this didn't make much sense, unfortunately ScriptingHelpers doesn't let me embed graphs, so I've skipped a lot of the mathematical explaination. Also I didn't get a chance to test them, so if I've messed one up then please drop a comment and I'll try and fix it.

2 comments

0

Thanks a bunch! I'll check it once I get home... my computer refuses to connect to my hotspot

0

Thanks a bunch! It works and it makes my gun animations look so much better!

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