I tried refactor this method on smaller pieces, and muse fall
Intresting why
public IEnumerator TravelPath()
{
IsMoving = true;
Game.Instance.Anomaly.AnomalyMoveAndInteractMovement.OnUnitStartMoveToHex(_thisUnit, ThisUnitLocation);
Game.Instance.Source.SourceMoveAndInteractMovement.OnUnitStartMoveToHex(_thisUnit, ThisUnitLocation);
Vector3 a, b, c = _pathToTravel[0].Position;
yield return LookAt(_pathToTravel[1].Position);
if (!CurrentTravelLocation)
{
CurrentTravelLocation = _pathToTravel[0];
}
_thisUnit.Grid.DecreaseVisibility(CurrentTravelLocation, _thisUnit.VisionRange);
int currentColumn = CurrentTravelLocation.ColumnIndex;
float t = Time.deltaTime * TravelSpeed;
for (int i = 1; i < _pathToTravel.Count; i++)
{
HexCell from = _pathToTravel[i - 1];
CurrentTravelLocation = _pathToTravel[i];
int moveCost = GetMoveCost(from, CurrentTravelLocation, default);
bool isNotEnoughMoves = Moves < moveCost;
if (isNotEnoughMoves)
{
Debug.Log("Is not enough moves " + Moves + " " + moveCost);
SetAsLocation(_pathToTravel[i - 1]);
break;
}
Moves -= moveCost;
// Debug.Log(" Moves " + Moves);
a = c;
b = _pathToTravel[i - 1].Position;
int nextColumn = CurrentTravelLocation.ColumnIndex;
if (currentColumn != nextColumn)
{
if (nextColumn < currentColumn - 1)
{
a.x -= HexMetrics.InnerDiameter * HexMetrics.wrapSize;
b.x -= HexMetrics.InnerDiameter * HexMetrics.wrapSize;
}
else if (nextColumn > currentColumn + 1)
{
a.x += HexMetrics.InnerDiameter * HexMetrics.wrapSize;
b.x += HexMetrics.InnerDiameter * HexMetrics.wrapSize;
}
_thisUnit.Grid.MakeChildOfColumn(_thisUnit.transform, nextColumn);
currentColumn = nextColumn;
}
c = (b + CurrentTravelLocation.Position) * 0.5f;
_thisUnit.Grid.IncreaseVisibility(_pathToTravel[i], _thisUnit.VisionRange);
for (; t < 1f; t += Time.deltaTime * TravelSpeed)
{
yield return LookForward(a, b, c, t);
}
_thisUnit.Grid.DecreaseVisibility(_pathToTravel[i], _thisUnit.VisionRange);
t -= 1f;
}
CurrentTravelLocation = null;
a = c;
b = ThisUnitLocation.Position;
c = b;
_thisUnit.Grid.IncreaseVisibility(ThisUnitLocation, _thisUnit.VisionRange);
for (; t < 1f; t += Time.deltaTime * TravelSpeed)
{
yield return LookForward(a, b, c, t);
}
_thisUnit.transform.localPosition = ThisUnitLocation.Position;
_thisUnit.Orientation = _thisUnit.transform.localRotation.eulerAngles.y;
_pathToTravel.Clear();
IsMoving = false;
Game.Instance.Anomaly.AnomalyMoveAndInteractMovement.OnUnitReachedHex(_thisUnit, ThisUnitLocation);
Game.Instance.Source.SourceMoveAndInteractMovement.OnUnitReachedHex(_thisUnit, ThisUnitLocation);
}