David
David

Reputation: 770

Unity 2D - A* Pathfinding how can I make seekers avoid other seekers?

Working on a Unity 2D top down game with the Aron Granberg A* pathfinding asset. Does anyone know how I can have the enemies with the seeker script avoid each other? Currently they will bunch up on one another and I would like to avoid that.

In the photo below you can see the green line that shows the AI Destination Setter target. It follows the player correctly but the right cube is trying to go straight through the red cube. How could I change it so the seekers avoid one another but still follow the player?

Seeker

    public class AIDestinationSetterHyperius : VersionedMonoBehaviour {
    /// <summary>The object that the AI should move to</summary>
    public Transform target;
    IAstarAI ai;

    public void OnEnable () {
        target = GameObject.FindWithTag("Player").transform;
        ai = GetComponent<IAstarAI>();

        if (ai != null) ai.onSearchPath += Update;
    }

    public void OnDisable () {
        if (ai != null) ai.onSearchPath -= Update;
    }

    /// <summary>Updates the AI's destination every frame</summary>
    public void Update () {
        if (target != null && ai != null) ai.destination = target.position;
    }
}

Upvotes: 0

Views: 1388

Answers (2)

Ross Marko
Ross Marko

Reputation: 31

I’m a noob in unity so my approach might be bad. A while ago I managed to get this working. So I added seekers’s layer to the list of what should be avoided, then modified the script where A* scans obstacles and made scan not only in the beginning but with a certain rate. Experimented with the scan rate until it became lite on performance but still did it’s job. My project was small so I had no issues and frame rate wasn’t dropping.

Upvotes: 0

djdanlib
djdanlib

Reputation: 22536

If I'm understanding this correctly, it looks like you want local avoidance which is only available in the Pro version of the package.

Upvotes: 1

Related Questions