Unity - Basic AI Enemy Follows Player - Prevent Flocking Of Enemies Together

I'm in the process of implementing a 2D top-down shooter game. I have a basic AI script that allows enemies to follow the player around. Currently it works fine for one enemy, but as soon as there are multiple enemies, overtime they tend to flock together. How would I go about fixing this issue? I think that one possible solution would be to make the enemies move in a random direction every few frames, but I'm not sure on this.

Below is my C# Script that I am currently using.

using UnityEngine;
using System.Collections;

public class AIScript : MonoBehaviour
{
public Transform target;
public int moveSpeed;
public int rotationSpeed;

void Start()
{
target = GameObject.Find("Michael").transform;
}

void Update()
{
if (target != null)
{
Vector3 dir = target.position - transform.position;
// Only needed if objects don't share 'z' value.
dir.z = 0.0f;
if (dir != Vector3.zero)
transform.rotation = Quaternion.Slerp ( transform.rotation,
Quaternion.FromToRotation (Vector3.right, dir),
rotationSpeed * Time.deltaTime);

//Move Towards Target
transform.position += (target.position - transform.position).normalized
* moveSpeed * Time.deltaTime;
}
rigidbody2D.velocity = Vector3.zero;
}
}

• Usually completing goals are reduced to vectors and then combined to produce a composite. You may enjoy reading The Three Simple Rules of Flocking Behaviors: Alignment, Cohesion, and Separation Mar 25, 2015 at 13:32
• oh hey thanks for the link. I've seen the original Boids site, but it's cool to have it all streamlined like this. Mar 25, 2015 at 16:10