I solved Enemy Error at Long Distance:

If you’re having trouble with the code not allowing the enemies to think past curved walls and use proper pathfinding in some circumstances, then I have a hack. There’s probably a better way to do this, but here’s what I did:

Make the Enemy’s Max Move Distance huge, like 25 units. Doing this will make the enemy follow the pathfinding algorithm to the closest grouping of characters. That solves the enemy correctly following pathfinding over distance.

Then, to make the enemy go the proper distance each action, I added the following code to the MoveAction script:

Here’s what I put near all the serialized fields (adding some variables for my update):

    private float enemyMoveDistanceTotal = 0;
    private bool startedMoving = false;
    private bool isUnitOnTargetPosition = false;

Then, I changed the Update to look like this:

private void Update()
    {
        if (!isActive)
        {
            return;
        }
        
        Vector3 targetPosition = positionList[currentPostionIndex];
        
        // I need these totals to keep track of how far the enemy moved as they move from each pathfinding node
        // to the next:
        if (!startedMoving && this.unit.IsEnemy())
        {
            float distanceThisTurn = Vector3.Distance(transform.position, targetPosition);
            enemyMoveDistanceTotal += distanceThisTurn;
              
            //I also want to make sure an enemy doesn't stop on another enemy (this needs work)
            isUnitOnTargetPosition = LevelGrid.Instance.HasAnyUnitOnGridPosition(LevelGrid.Instance.GetGridPosition(targetPosition));
        }
        startedMoving = true;

        Vector3 moveDirection = (targetPosition - transform.position).normalized;
        transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotationSpeed);
        float stoppingDistance = .1f;
        if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
        {
            transform.position += moveDirection * moveSpeed * Time.deltaTime;
        }
        else
        {
            currentPostionIndex++;
            startedMoving = false;
            if (isUnitOnTargetPosition)
            {
                isUnitOnTargetPosition = false;
                return;
            }
            // in addition to checking if my positionList is depleted,
            // I'm also checking to see if the total the enemy has moved has exceeded my 
            // enemyMoveDistance, which will also cancel the move-loop,
            //also, because the enemyMoveDistanceTotal never goes past zero for a nonenemy, the added 
            // code will ignore allies:
            if (currentPostionIndex >= positionList.Count || enemyMoveDistanceTotal >= enemyMoveDistance)
            {
                
                enemyMoveDistanceTotal = 0;
                OnStopMoving?.Invoke(this, EventArgs.Empty);
                ActionComplete();
            }
            
        }  
    }

Privacy & Terms