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;
}
}