RTS game development - Week #2
Tags: csharp, unity3d, gamedev
Published: 2019-10-27 19:56:02

Repo: https://gitlab.com/Kipash/rts-concept

It's a week 2, after a pause, I had to go back to this awesome project. Let's get into it.

The key thing I searched for and finally found is...Order & assign algorithm, which I've seen in a part of this GDC talk from 2014. (Around the 11th minute, the guy on the iPad) https://www.gdcvault.com/play/1020832/The-Simplest-AI-Trick-in

So the basic idea is to split the formation into layers based on the orientation of the formation. You create these slots in different layers.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9o95p4ueewc3b9wsl80d.png)

Then, you layer the actual units, but in the same direction as you layered the slots.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/7t8qgk7awxd6n9wl1sdr.png)

Then it's simple to assign a unit to a slot. Well, it should be. I haven't listened to the man's advice and so I've implemented the last step differently. I'm going to look into it in the future.

This is how it works. It finds the intersections with the perpendicular lines based on the unit position and cross product of the formation direction.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/rjxrv9hdk362r28cgs5p.png)

Just a meaningless snipped. I would advise you to check out the source with the full context (https://gitlab.com/Kipash/rts-concept).

```c#
//Create intersections
var dirStart = formationOrigin;
var dirEnd = formationOrigin + formationDirection;

List<Intersection> intersections = new List<Intersection>();
foreach (var unit in SelectedUnits)
{
    var uPos = unit.transform.position;
    var l = new Line(uPos, uPos + horizontalDirection);

    if (LineSegmentsIntersection(
            new Vector2(dirStart.x, dirStart.z),
            new Vector2(dirEnd.x, dirEnd.z),
            l.A,
            l.B,
            out Vector2 intersection))
    {
        intersections.Add(new Intersection() { Point = intersection, Unit = unit });
        print($"inter: {unit.GetUnitID}");
    }
}
```

So what else is new? First I had to get rid of the green sprites since they were awful. I've downloaded a 3d model of a soldier, just to have a scale. I've added a visualization where the unit will arrive for debugging purposes, but I might leave this in-game. Then just a quick mock of UI and the proportions. The first thing the UI was used for was the formations, so you can switch the type.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/g4rtgb8nhbjadu0n0a79.gif)

##Current bugs##

Let's start with the mentioned sorting. Right now it cleverly sorts them into layers, but in the layers, the order is greedy and not clever.

If the player wants to select one unit, it has problems on different resolutions. Currently, it goes through all the units and checks the position. I think it might be cheaper to change it to a simple ray cast.

##Features to add##

Unity's NavMesh agent controls the rotation, but this way you can't determinate the final rotation uppon arriving.  For example in the Circle formation, every man is looking outwards from the circle, but if you sent the units to that destination, they'll be staring in the same direction in which they arrived. So, custom rotation and smoothing have to be added so the custom final rotation could be used.

Unit grouping. UI should have dynamic buttons for every group of units. With that goes the selection, while in the group mode, if you select a unit, you'll select the whole group. Then, with a button, you'll go back to the individual mode and you're able to select and control individual units.

---------------------------------

I'm having a blast. See you next week.

Shout out to Brian Peek for nice game industry-related news.
{% user brianpeek %}