RTS unit formation

I’m having a problem whenever I drag multiple units into one point, the units keep on bumping each other fighting for one position.

Can someone give me any idea?Thanks!

How do you mean dragging units into one point? Do you mean clicking and they all go to the same place?

You need to setup a group leader.

For instance if you have 7 objects selected, and move them to a certain position, automatically assign one of those as the leader, and give each other object in the group an offset to the leader. This works well with objects of the same size, however for different sized objects it can get tricky.

Another way to do it is as each object arrives at the target position, simply find the object that will arrive next and work out a suitable offset for it, then do this with each next arriving object.

Like Meltdown said, there’s actually a couple of things you can try.

The best way I’ve found seems kind of complicated, but it’s actually pretty easy when you get down to it. Basically, whatever units you have “selected”, you can find their relative center point. And pretty much no matter how far apart they are, or where they are at in your scene. It just requires some simple math.

  1. Find the relative center point of your selected units: Add all of your units position vectors (transform position) with something like this
var total : Vector3;
 				for (var unit : GameObject in listSelected) {
 					if(unit != null) {
 					total += unit.transform.position;
 					}
 				}
  1. Find the center point of your selected units by dividing your vector3 “total” by the number of units selected (usually in an array) with something like this:
//finding the center point of all the units positions
 				var center : Vector3 = total/listSelected.length;  //Center point of grouped units

You now have the center Vector3 position of your grouped units. And with this Vector3, we can easily “offset” each unit by a certain vector in order to keep them spaced out at there endpoint or destination vector.

  1. Find each units “starting” position (before you move them) with something like this:
var startVector : Vector3;
startVector = unit.transform.position - center;

Then, simply add the starting vector to the endpoint (or destination point) of your unit’s path. Like this (assuming a raycast to determine your endpoint):

newEndpoint = raycasthit.point + startVector;

So to sum up. Find your selected groups center. Find how far your unit is from the center (using vectors). Add that vector onto the endpoint of your groups path. Anyways, it works, but there are other ways as well.

There’s also a very good video tutorial on Youtube that covers things like this, just search for “RTS binary”.

4 Likes

Yes exactly, Thank you very much for the idea meltdown! I’ll try it.

Thank you very much velo!I appreciate you help.