Moving X amount of units to destination, RTS style movement

I have a reached a point now where I can select X amount of units, and move them to where I click. My problem is, they all try to get to the same point.

I use Unity’s own Navmesh + agent.

I set destination like so:

public bool MoveToPoint(Vector3 point)
   {
       if (SamplePosition(point))
       {
           agent.SetDestination(point);
           return true;
       }

       return false;
   }

I tried a system where I send an event when ANY agent reaches the point, then all agents stop, but realised quickly that will never work well.

I’ve read the material I can find through Google but nothing really helps me get started.

If I have a List of units, all with an attached agent, how can I move these to a point while keeping some sort of logical formation, no matter how far apart they started?

It does not have to be very fancy, I just want them to stop bumping into each other and doing that weird “Spassing out” thing they do when they can’t reach their destination cus they are in each others way.

To have really good control of the units you will probably need to do your own pathfinding and control of priority between the units.

There may be some ways to put something on top of Unity’s navmesh agents to make them behave better, such as also looking for nearby units and always having a pecking order: each unit would get a different number, and higher-numbered units would yield to lower ones, but that does not solve all problems: if a low priority unit reaches a door and another high priority agent bumps into it, the low one would stay indefinitely.

Pathfinding is definitely a Hard Problem™, especially when your agents are something that you expect to move a certain way, such as a horde of humans. Navmesh agents generally don’t move like humans, alas.

here is a good tutorial :slight_smile:

1 Like

Thread locked.