2
\$\begingroup\$

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;
    }
}
\$\endgroup\$
2
3
\$\begingroup\$

One simple way of doing it could be to make a few alternate versions of the script and have different AI profiles for the enemies (just give them a random one when created). This way you could have one type that goes straight ahead like you have now, but also others that try to get the player by curving right or left. Maybe even one that makes some random moves if that works in your game.

I don't think this is a perfect solution, but at least it would break up the enemies in smaller flocks without too much effort on your part.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This worked well for Pac-Man! \$\endgroup\$ – Kelly Thomas Mar 25 '15 at 15:27
0
\$\begingroup\$

You could try giving each enemy a mass in Unity and collision detection so they cant go thru each other, but without a lot of work if they are doing basic player tracking you are going to end up with a horde. You could also set it up with an if statement to where before they move it checks the area for other enemies within a certain radius. Then set it to look for a new space new space if that one is full or to move if it is empty.

\$\endgroup\$
0
\$\begingroup\$

You want a combination of behaviours. Use vectors to calculate an attraction to the player, and a repulsion from other nearby enemies. Weight those behavioural vectors to make them stronger or weaker, and then combine them together using vector addition. The resultant vector is the place the AI wants to be; you can then reverse engineer that vector into a direction and speed if that's how your AI move.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.