CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Follow publication

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!

Mina Pêcheux
CodeX
Published in
18 min readDec 1, 2022

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…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Mina Pêcheux
Mina Pêcheux

Written by Mina Pêcheux

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)

Responses (1)

Write a response