Unity RTS Engine [Asset] [WIP]


Short Description:

This RTS Engine Asset handles all the game management for you and offers various possibilities of customization providing you the chance to create your RTS game from the inspector. It handles everything from maps, factions, NPC factions (AI), resources, units, buildings, minimap, UI, selection, single player, multiplayer, etc…

Demo:

WebGL Demo: Click here.
Feedback is always appreciated!
A standalone is also available, PM me to get it!

Features:

- Maps:
Each map must have each own scene where you can set the map as a normal terrain or any other form you want. Map settings include: the maximum amount of allowed factions per map, peace time, resource management, etc…

- Factions:
Have as much factions as you want in each map and customize each faction by a unique name, color, initial population, max population, capital building. While you can make factions share the same units and buildings, they can also have their own unique buildings and units. A player is defeated only when his faction’s capital is destroyed. In a single player game, one faction is controlled by the player and others are controlled by the AI who takes care of the resource management, building management, recruiting an army to attack or defend.

- AI:
There are different components that handle the multiple task that a NPC faction needs to deliver. From collecting resources, constructing buildings to assembling an army and attacking other factions. The AI will attempt to eliminate other factions by creating army units. The AI relies on some values that you set that will determine their behavior. This behavior determines how the NPC factions collects and uses its resources, how it takes its attacks and plans its strategies.

- Resources:
Create and customize different types of resources that unit and building creation depend on. Place the resources on the map and make their quantity limited or infinite and play with other settings such as the maximum amount of collectors, collection speed, etc…

- Buildings:
Customize buildings by making them deliver different tasks such as creating units, increasing the total maximum population, adding resource collection bonuses. Change the buildings look depending on its state (current health) to highlight damaged buildings for example or show a different look for under construction buildings. Other settings include: amount of possible builders and their interaction with the building, max allowed tasks, target positions for newly created units. Set a reload time and required resources for each task in addition to other customization. Buildings can also expand the faction’s territory to allow to exploit more resources and bring more space for the faction.

- Units:
Every faction needs units who can deliver multiple tasks, right? The Unit component defines the unit (name, code, health and all general settings) and handles the unit’s movement. A unit can have one function or many:

  • Construct new buildings and fix damaged ones.
  • Collect resources for the faction.
  • Attack other factions’ units and buildings.

- Minimap:
The minimap shows the whole map at once as well as the borders of each faction, clicking on any area on the minimap will move the main camera view to that position allowing for fast navigation on the map.

- Camera:
RTS Camera that includes basic things like zoom using the mouse wheel or keyboard keys, moving on screen edge, movement limits and customization for the movement sensitivity and keys.

- Selection:
Selecting a unit or a building can be simply done by a left mouse click on their objects. Multiple selection is also available by holding the left mouse button and dragging the mouse. Right clicking on a building, resource or other unit while selecting unit(s) will make the units collect, build or attack depending on the unit’s abilities and the nature of the target.

- UI:
The game’s UI includes:

  • a menu that the player’s faction resources and current/maximum population.
  • the Task Panel that shows the available tasks for the selected unit(s) or building.
  • the Selection Panel shows the currently selected unit(s) or building and displays info about them as well as their current health. In a case of a building, pending tasks will also appear in the selection panel.
    (Check the demo for a detailed explanation about each UI panel).

- Single Player Menu:
The menu includes a component that handles picking out the map, choosing how many factions to spawn and determining the settings of these factions (color, name and for NPC factions: the difficulty level). You can have as much difficulty level as you want, each difficulty level represents different settings for the three AI managers above. One difficulty level doesn’t lead to the exact same actions but instead two factions having the same difficulty level can be different but they will generally some small similarities.

- Multiplayer:
Multiplayer using UNET using a lockstep model (both local games and internet match making available).

- Code modification and custom events:
Custom events are events called when a major action happens in the game (creating a new unit, placing a new building, etc…) to allow for even more customization.

There are a lot more features than what I have written above but I tried to keep the thread short. The asset’s documentation will include every little detail of the asset.

1 Like

Great… When are you going to release?

Thanks! Right now, I am working on the Multiplayer side of the asset. I can say it is 70℅ done. Hopefully I will be able to release the asset by the beginning of January.

I am currently working on the multiplayer menu and the network lobby management.
A list of things that are available in the MP menu:

  • Creating and joining a local or an internet match.
  • The match creator is the only one able to pick the map for the game and limit the maximum amount of factions.
  • Just like in the single player, each player picks a name and a color for their team.
  • The match creator is the only one able to launch the game if all other players have been marked ready.

Things to add:

  • Kicking players from the lobby.
  • Handling disconnection errors.
1 Like

More work has been done for the multiplayer menu and in game synchronization:

  • The lobby creator can now kick players.
  • Everything is now synced correctly between the players in the room.
  • Player disconnection effects on the game have been added.
  • Players move to the game scene and faction initialization syncs successfully
  • Syncing building placement and building states between players in the game works correctly.

I’m actively testing the multiplayer and trying to hunt bugs and fix them. I also try as much as I can to minimize the data transferred between the players so it does not get heavy on anyone’s connection.
My main task today/tomorrow is to sync the units movement. I will keep updating this thread and maybe post new screenshots!

I’ll be eagerly following your development. There’s a couple of RTS kits on the asset store, but they all fall short. Here’s hoping you can be the one to bring a fully fledged RTS kit to the store.

Would there be an option to require gatherers to drop resources off at certain buildings, like in Age of Empires, rather than have the resources automatically add to the stockpile as they harvest them?

1 Like

Thanks for your replies! Yeah, that type of resource collection will be available in the asset. You will be able to choose between those two types actually. But currently, I prefer to invest all my time into the multiplayer. Since it’s not really going to affect how the multiplayer game works, I preferred to add it later.
It’s always encouraging to see someone interested in the asset so keep those suggestions coming please!

Today, I completed some unfinished work in the multiplayer feature. In addition, the reported bugs that I received have been fixed. My focus now is to implement the second type of resource collection just like Ronald described above. After that, I will upload a new demo that will include the new progress and probably the multiplayer feature!

I’m planning to finish the work for the initial version of the asset before new year’s. After new year’s, I only want to write the documentation and prepare the asset’s listing. Hopefully, no weird bugs get in the way of making that!

Have you done any calculations on bandwidth requirements for multiplayer? I imagine that syncing the hundreds of units in play in a typical RTS would be quite demanding, and would increase significantly for each extra player you add. In the game I’m working on which is a tower defence, RTS hyybrid, I’m bumping up against my bandwidth target of 256K up/down, and thats with a 2 player limit and up to 150 units in play at once.

Some of the compression/optimisation techniques I’m using are:

  • Compressing position data to 2 16 bit integers (shorts) for x and z position. My levels are 256 * 256 metres, so this gives a precision of about 4mm, which I deem good enough, at least in my case. Z position is clamped to terrain so that can be calculated on the client rather than synced over the network.
  • No sending of rotation data. This I calculate by simply lerping units rotation to face in their movement direction, or face toward the unit they are attacking. It’s not necessarily precise, but the rotation doesn’t actually effect gameplay in my case.
  • Don’t sync movement of physics based projectiles. Simply start them off with the same velocity and rely on the physics engines to be close enough to not make the simulation end up too different on each machine. Just to make sure though, I only calculate the damage done by the projectile on the host and send that to the client, so on the odd chance that the physics simulations don’t match, at least gameplay wise it won’t matter.

Generally RTS games use a lockstep system so that only commands need to be synced rather than unit positions, but with Unity, that’s an endeavour that’s so far eluded everyone who’s tried (at least as far as I know). It’s certainly far beyond my abilities.

There are articles floating around with implementations using the legacy networking architecture. Porting the concept to UNet isn’t trivial but it’s certainly possible. We have a working lockstep simulation in UNet.

That’s great, I’m glad to be proved wrong on this one. Do you have any plans to make a system available on the asset store, or would it be too game specific for that?

EDIT: The below explanation of the multiplayer game does not represent how it works in the current build. I decided it was not ideal for a RTS game and went for another model.

Thanks a lot for your comments guys. I think I should explain how the current Multiplayer works.

  • Things like spawning units and buildings are handled by a request that the client sends to the server which will then spawn the required object to all clients and assign authority to one of them. Same applies to destroying buildings and units.
  • After spawning buildings, only the health is synced between the clients. Everything also is local. When the player’s building receives damage. A request to update the building’s health is sent by the building’s owner to the server which will then update the health for all players.
  • Attack objects are also handled by the server which spawn them from the pool depending on the clients’ requests.
  • Syncing audio and animation is also done in the same way.

As for the player movement, the syncing is different. There’s a cycle that collects each player’s movement orders and when this cycle ends it sends the movement orders to the server who will then spread them across all the clients. When all clients report to the server that they have received the movement orders to perform in that cycle, the server will give permission to perform this actions. This means that when performing a unit movement, all the clients have the same movement orders. BUT, when a client fails to receive the permission to perform the movement order and other clients don’t. This client falls a little behind. I tried to fix this by freezing the game for all clients when one falls behind and could not inform the server about receiving movement orders. However things are still not ideal. Sometimes, when a client to decides to change the movement destination of some already moving units and he fails to send the movement order to other clients quickly. The units initial position will have to change as their owner requests the movement to start from. I’m not sure if this is a normal behavior because while playing a lot of multiplayer games, when a player is laggy while moving, he would actually keep moving towards the same direction till he can communicate with the server again and his position will snap to the one when he locally has.

In the beginning, I was hesitant to add multiplayer to the asset but I wanted to give it a shot. The other problem is that, in a local game, everything works great. However, using UNET lobby matches, I am not sure if I’m getting the real or not. I’m currently using the free plan and it feels very slow, even for projects when I have no info transferring between clients, it fails a lot. Sometimes, I get timeouts just by leaving the game idle and not performing any action from any client. Is this something common?

I might consider it when Fractured State is finished and I actually have time to think about anything else :slight_smile: We do sync a bit more than your typical RTS because our damage has some degree of randomization so the host is doing dice rolls and propagating those results to clients. Most RTS games (as far as I know) don’t sync damage; just Unit A is attacking Unit B which is fine when a weapon always does the same amount of damage and always fires at the same rate.

So you’re lockstep…ish. I’m curious why you bothered to lock step move commands but nothing else? You can still run into sync issues if, say, a unit is logically dead but still alive and dealing damage on a single client’s simulation. I also don’t see any reason to sync animations or audio unless they’re important to your game mechanically.

We don’t pause the simulation itself when we encounter latency but player command processing does pause until the offending client catches up. This manifests itself as other players, for example, issuing a move order and those units standing in place for a bit.

Not sure about the slowness problems with Unet, since I only use Photon at the moment. As for the failed movement orders causing freezing, I think the normal solution is to intentionally build in a bit of lag, so that when you give an order, it takes say, 200ms before the unit reacts. This way by the time the order is actually executed on the client giving the order, the command would have had plenty of time to get to the other players, including being resent if necessary.

If you haven’t read this article, it probably has a better explanation of this than I give: Lockstep Implementation in Unity3D | Clinton Brennan

Thanks for your input guys. I’m familiar with lockstep multiplayer but I only realized, after avoiding it, that it’s the only solution for a RTS game. Therefore I decided to continue working on the single player side of the asset with lockstep in mind to make it easy to implement later. I will be doing some tests this week to see how long will it actually take to implement a lockstep system. If it’s going to take a long time, I will be releasing a single player version of the asset first and then the multiplayer will follow up.

1 Like

So good news guys, the asset will be released with multiplayer! I was able to scrap the old multiplayer and transform it in a lockstepish model. Currently a lot less data is transferred between clients each cycle and the game is syncing pretty good. I have also added the second type of resource collection as Ronald suggested.
I will upload the new demo (with multiplayer) in a few hours.

3 Likes

Sorry for the late update but I was working on a lot of stuff in order to bring the multiplayer to a stable state. And it’s now available in the WebGL demo. If you want to try the standalone demo, please PM me. I’m awaiting your feedback!
Right now, I am cleaning up the code, improving it and writing the documentation. Will keep you updated as always.

2 Likes

The demo has been updated to the latest version of the asset. I’ve been focusing on testing the multiplayer system and trying to improve it. Although it’s looking in a pretty good state right, there’s still room for changes and improvements. Moreover, I’ve been fixing some bugs here and there.
Big thanks to all the testers who helped me with their feedback and suggestions.
Hopefully, the asset will be released by the end of January.

2 Likes

I can say that the initial version of the asset is 99% ready. I’ve also finished all the documentation and I am currently preparing the release. The demo has been updated as well and If you want to test the standalone build, please drop me a PM!

2 Likes