Member-only story
Using behaviour trees for a RTS collector AI in Unity/C#?
Let’s discover how to give life to little trucks thanks to this AI design pattern!

This tutorial is available in video or text format — see below for the text version :)
Check out the Github repo with all my Unity tutorials! 🚀
So, a while ago, I made this tutorial about the fundamentals of behaviour trees. In this article, I first explained the theory behind this common AI design pattern, then I showed how to create a basic C# abstract library to implement them in a Unity project, and finally I went through a little example of a guard patrolling AI system.
Today, I want to continue on this topic and discuss how we can use our behaviour tree library to implement a slightly more complex AI: a collector agent for an RTS game!
The point here will be to have little trucks that spot and navigate to nearby resources, chop down trees or mine rocks until their storage is full, then go to the nearest storage center and deliver the goods, before returning to the closest resource location, and doing another round.

Collectors will be of two types, depending on the type of resource they gather: blue trucks will get wood from trees, and orange trucks will get minerals from ores.
Also, the resources will deplete after a while — for example, once they are “dead”, trees will get another sprite and will be ignored by the collectors. So these collectors will work as long as there are resources of the right type on the map; or else simply return to the nearest storage center and enter the building.
🛠 Tools & setup
To implement all of this, I will rely on the library we coded previously — I’ve just made a few improvements or adjustments like adding a…