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?
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.