I want to create a smooth turn using lerp, which I have already made, and it is working.
However, if one of the axis is -180°, some of the values change to 0°, which is bad as it loops all the way around, causing this to happen:
However, Im not sure if there is a workaround, such as not using :ToOrientation()
, and instead using :ToEulerAnglesXYZ()
Here is my code so far:
The moving of the enemy
EnemyData.DeltaTimeAdded += DeltaTime
print(math.deg(select(2, EnemyData.OldCFrame:ToOrientation())))
print(math.deg(select(2, EnemyData.CFrame:ToOrientation())))
EnemiesActive[EnemyID].PrimaryPart.CFrame = CFrame.new(
Lerp(
EnemyData.OldCFrame:ToWorldSpace(CFrame.new(EnemyData.XOffset, 0, 0)).Position * Vector3.new(1, 0, 1),
EnemyData.CFrame:ToWorldSpace(CFrame.new(EnemyData.XOffset, 0, 0)).Position * Vector3.new(1, 0, 1),
EnemyData.DeltaTimeAdded / EnemyData.OverallDeltaTime
) + GetUpHillValue(EnemyData, Lerp(
EnemyData.OldCFrame.Position * Vector3.new(1, 0, 1),
EnemyData.CFrame.Position * Vector3.new(1, 0, 1),
EnemyData.DeltaTimeAdded / EnemyData.OverallDeltaTime
)
) ---This is the part that's erroring ⬇️
)* CFrame.fromOrientation(Lerp(EnemyData.OldCFrame:ToOrientation(), EnemyData.CFrame:ToOrientation(), EnemyData.DeltaTimeAdded / EnemyData.OverallDeltaTime), Lerp(select(2, EnemyData.OldCFrame:ToOrientation()), select(2, EnemyData.CFrame:ToOrientation()), EnemyData.DeltaTimeAdded / EnemyData.OverallDeltaTime), Lerp(select(3, EnemyData.OldCFrame:ToOrientation()), select(3, EnemyData.CFrame:ToOrientation()), EnemyData.DeltaTimeAdded / EnemyData.OverallDeltaTime)) --- This is the part i need help fixing
How I get the EnemyData CFrames:
EnemyData.OldCFrame = EnemyData.CFrame
EnemyData.CFrame = GetNode(EnemyData.Node, EnemyData.Path).CFrame:ToWorldSpace(CFrame.new(0, 0, (EnemyData.CFrame.Position - (GetNode(EnemyData.Node, EnemyData.Path).Position)).Magnitude - (EnemyData.Speed * OverallDeltaTime))) * CFrameAngles(EnemyData)
How I get the angles:
local function CFrameAngles(EnemyData)
local x, y, z = EnemyData.CFrame:ToOrientation()
return CFrame.fromOrientation(CFrame.new(GetNode(EnemyData.Node - 1, EnemyData.Path).Position, GetNode(EnemyData.Node, EnemyData.Path).Position):ToOrientation(), y, z)
end
What the output usually looks like:
Top one is print(math.deg(select(2, EnemyData.OldCFrame:ToOrientation())))
One underneath is print(math.deg(select(2, EnemyData.CFrame:ToOrientation())))
180.000010212 ---Top
180 ---Under
0.0000005 --- Top
180.000010212 --- Under