[AI] Enemy spreading out code causes jittering.

So I created a foreach loop in C# inside unity that calls for my enemies to spread out and prevent them from clumping together. However it has been causing my robots to jitter especially when they are retreating from the player.

So here are the codes that allows my robots to spread out.

public void AvoidEnemies()
    {
        foreach (var enemy in EnemiesNearby)
        {
            // Find distance between robot and other enemies
          
            float enemyDistance = Vector3.Distance(transform.position, enemy.transform.position);
            // Find distance between other robots and player
            float robotsDistanceToPlayer = Vector3.Distance(enemy.transform.position, target.transform.position);
            if (enemyDistance < minEnemyDistance)
            {
                cluttered = true;
                if (retreating == false) // Don't spread out yet if retreating
                {
                    if (state == RobotState.STANDBY || state == RobotState.STANDBY_LOW)
                    {
                        transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x + (transform.position.x - enemy.transform.position.x), transform.position.y, transform.position.z + (transform.position.z - enemy.transform.position.z)), movementSpeed * Time.deltaTime);
                        // Include some way of making enemies move away from each other
                    }
                }
            }
            else if (enemyDistance >= minEnemyDistance)
            {
                cluttered = false;

                // While enemy distance is more or equal to minEnemyDistance, if its distance is within a small distance to
                // minEnemyDistance, prevent them from moving causing jittering
                if (enemyDistance <= (minEnemyDistance + 1f))
                {
                    closeToMinDist = true;
                }
                if (enemyDistance > (minEnemyDistance + 1f))
                {               
                    closeToMinDist = false; // false if more that a 0.5f for away from min enemy dist
                }
                // If robot is further away from player than other robots & closeToMinDist is true
                if (targetDistance > robotsDistanceToPlayer && closeToMinDist == true)
                {
                    // Ensure its in Stanby and Standby Low State
                    if (state == RobotState.STANDBY || state == RobotState.STANDBY_LOW)
                    {   
                        // Make robotstop moving towards player unity robot infront is out of range
                        stopMoving = true;
                    }
                   
                }
                else
                {
                    // Or else keep moving
                    stopMoving = false;
                }
            }
           
        }
    }

I tried creating a float that compares the distances the robots have from the player and forces them to stop if the other robot they are near to is closer to the player than itself. However it is still causing jitterings to happen.

This is the code that controls the movement for the robots.

// Robot movement Script
        if (playerScript.playerHealth > 0 && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Damage"))
        {
            // Follow player
            if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack") && robotHealth > 0)
            {
                // prevent robots from moving straight into player & stop moving is bool is decalred true & not retreating
                if (targetDistance >= minTargetDistance && stopMoving == false && retreating == false)
                {                  
                    transform.position = Vector3.MoveTowards(transform.position , target.position, movementSpeed * Time.deltaTime);
                }

                // Make the robots retreate when they too close to player && not engaging
                if (targetDistance < retreatingDistance && state == RobotState.STANDBY || targetDistance < retreatingDistance && state == RobotState.STANDBY_LOW)
                {
                    retreating = true;
                    if (facingRight)
                    {
                        transform.position = Vector3.MoveTowards(transform.position, retreatLeft, movementSpeed * Time.deltaTime);
                    }
                    else if (!facingRight)
                    {
                        transform.position = Vector3.MoveTowards(transform.position, retreatRight, movementSpeed * Time.deltaTime);
                    }     
                }              
                else if (targetDistance >= retreatingDistance)
                {
                    retreating = false;
                }
              
               
            }
        }

Retreat left and right are Vector 3 creating points for the robots to travel to when the player is too close.

        retreatLeft = new Vector3 (transform.position.x - 5, transform.position.y, transform.position.z + (transform.position.z - target.position.z));
        retreatRight = new Vector3(transform.position.x + 5, transform.position.y, transform.position.z + (transform.position.z - target.position.z));

Is there a way for me to fix this?

It doesn’t look like jitter to me. It looks like they’re playing the first frame or two of their movement animation, which has their heads way out of in front, and then rapidly changing back to their idle. You’ll need to add some sort of buffer distance of “OK it’s really far enough away, not just 2 pixels, start actually moving.”