I am trying to lerp an item to perform a full 360 rotation about the y-axis about its origin once. However, if I make it lerp 360 in a single motion, it ends up not moving at all. I assume that it is the system thinking I didn’t move since my final position is the same as my start. Is there a possible way to make it do a smooth and continuous 360 movement? Maybe lerping multiple times or any other way? And no, I don’t want to tween or anything, it has to be lerp.
1 Like
I would use tweens, but if you insist,
local Part = workspace.Part
for i = 1,360 do
Part.CFrame = Part.CFrame * CFrame.Angles(0,math.rad(1),0)
task.wait()
end
or
local Part = workspace.Part
function Lerp(a, b, t)
return a + (b - a) * t
end
local degreesOfRotation = 360
for i = 1,degreesOfRotation do
Part.Orientation = Vector3.new(0,Lerp(0, 360, i/360),0)
task.wait()
end
I have trouble the same problem, I have created this lerp function to lerp angles (-180 - 180):
function LerpAngle(A: number, B: number, Time: number): number
local AngleResult: {
MaxAngle: number,
Cost: number,
StartTime: number,
EndTime: number
}?
-- Check if can do angle lerp
for I = 1,2 do
-- Check if cost is lower than normal method
local MaxAngle = I == 1 and 180 or -180
local AngleA = MaxAngle > 0 and math.abs(A) or -math.abs(A)
local Cost: number
if MaxAngle == -180 then
-- 180 --> -180
Cost = math.abs(180 - A) + math.abs(-180 + math.abs(B))
else
-- -180 --> 180
Cost = math.abs(180 + A) + math.abs(180 - math.abs(B))
end
local COST_IS_HIGHER = not AngleResult and Cost > math.abs(B - A) or AngleResult and Cost > AngleResult.Cost
if COST_IS_HIGHER then
continue
end
-- Calcule lerp time
local StartTime = math.abs(MaxAngle - AngleA) / Cost
local EndTime = (180 - math.abs(B)) / Cost
if StartTime < 0 then
EndTime -= StartTime
StartTime = math.abs(StartTime)
end
if EndTime < 0 then
StartTime -= EndTime
EndTime = math.abs(EndTime)
end
AngleResult = {
MaxAngle = MaxAngle,
Cost = Cost,
StartTime = StartTime,
EndTime = EndTime
}
end
if not AngleResult then
-- If not triggered angle lerp, then calcule default lerp
return A + (B - A) * Time
end
-- Calcule angle lerp
local MaxAngle = AngleResult.MaxAngle
if Time <= AngleResult.StartTime then
local TimeA = Time / AngleResult.StartTime
local Increase = (180 - math.abs(A)) * TimeA
if MaxAngle == -180 then
return A + Increase
else
return A - Increase
end
return
end
local TimeB = 1 - ((Time - AngleResult.StartTime) / AngleResult.EndTime)
local Decrease = (180 - math.abs(B)) * TimeB
if MaxAngle == -180 then
return B - Decrease
else
return B + Decrease
end
end
Work like a normal lerp function:
local CurrentAngleY = LerpAngle(StartY, GoalY, Time)
I hope can help ![]()