Ultimate Guide: Developing Enemy AI for RTS Games in Unreal Engine
Players generally want something to fight against, so learning Unreal Engine enemy AI can be crucial to making your game dynamic.
RTS games are a fantastic genre, requiring quick-decision making alongside tense action – gaining players’ attention for hours on end. BUT – it wouldn’t be unreasonable to say that RTS games only work if you have good Unreal Engine enemy AI to go along with it.
In this tutorial, we’re specifically focusing on creating basic Unreal Engine enemy AI units. We’re going to work on the ability of the enemy units to attack the player units in the game. The enemy units will, namely, search for the player units in range to set one of them as the current target and get to attack it until it’s destroyed. We’ll also cover how to update the remaining player units as they are knocked down over time.
Let’s dig in!
Table of contents
- Before You Begin & Project Files
- Creating The Variables For The Enemy Unit
- Finishing Up The Enemy Unit Blueprint
- Conclusion – Unreal Engine Enemy AI
Before You Begin & Project Files
Basic skills in Unreal Engine and its Blueprints Visual Scripting system are needed for this tutorial. Notice that you’ll need to already have a player unit blueprint in your project as this is not covered in the present tutorial. Also, we’re using inheritance to make it easier to implement both player and enemy units, which can then use or overwrite functions and values from their parent class.
You can download a copy of the source code files for the project done in the tutorial here.
Note: This tutorial was made with Unreal Engine version 5.0, so if you have a higher version you might see some elements as deprecated although they still work.
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
AVAILABLE FOR A LIMITED TIME ONLY
Creating The Variables For The Enemy Unit
We’re going to start our Unreal Engine Enemy AI tutorial by creating the scripts that will set the Target Actors for the Enemy Units. We will do this in the EnemyUnit blueprint’s Event Graph.
To do it, let’s add new variables to the EnemyUnit’s Event Graph. First, we will add a new Float variable ‘DetectRange‘ with the default value of 500:
We will use that variable to set the distance at which an Enemy Unit will notice a Player Unit. Then, we will add a new variable called PlayerUnits of type PlayerUnit. We will store all of the Player Units in that variable, so we can find the one we can reach:
To store all of the Player Units in that variable, we’ll have to turn that variable into an array first. Arrays allow us to store not one but several elements in one variable. Here’s how we will turn the PlayerUnits variable into an array:
- With the PlayerUnits variable selected, go to the Details panel
- To the right of the Variable Type selector, you should see one more selector. It should have a head as an icon
- Click on that selector and select the Array option from the drop-down menu, to turn the PlayerUnits variable into an array of the Player Units
Filling Up The PlayerUnits Variable
Next, we want to fill the PlayerUnits variable at the start of the game. But first, let’s take a look at the EnemyUnit’s BeginPlay node. It is connected to the Parent: BeginPlay node. That means, that at the start of the game, the EnemyUnit first runs the script the Unit blueprint has at the BeginPlay node:
Now, let’s make our EnemyUnit fill the PlayerUnit variable after running the parent’s code:
- Connect the Parent: BeginPlay node to a new Get All Actors Of Class node
- Set the Actor Class pin of that new node to the PlayerUnit class
- Connect the output control flow and value pins of the Get All Actors Of Class node to a new Set Player Units node
With that script, at the start of the game, the Enemy Unit searches for all of the Player Units and saves them to the PlayerUnits variable:
If you start the game now, you won’t see anything happen, as we don’t actually do anything with those Player Units as of now.
Creating The Target Finder Function
We’ll use the Player Units we found to select one of them that we can use as a Target. Let’s create a new function called Try Find New Target for that:
As the game goes on, some of the Player Units might get destroyed. Let’s filter them out:
- Connect the Try Find New Target node to a new For Each Loop node
- Connect the Array pin of the For Each Loop node to a new Get Player Units node
- Connect the Loop Body control flow pin of the For Each Loop node to a new Is Valid node
- Connect the Array Element pin of the For Each Loop node to the Input Object pin of the Is Valid node
This way, we loop through each of the Player Units, check which of them still exists, and fire the Is Valid pin for them:
Then, we’ll find the Player Unit which is close enough to our Enemy Unit:
- Connect the Is Valid pin of the Is Valid node to a new Branch node
- Create a new Get Distance To node
- Connect the Array Element pin of the For Each Loop node to the Other Actor pin of the Get Distance To node
- Connect the Return Value pin of the Get Distance To node to a new Less Equal node
- Connect the second input pin of the Less Equal node to a new Get Detect Range node
- Connect the output pin of the Less Equal node to the Condition pin of the Branch node
This way, we’re checking the valid Player Units on being closer than the Detect Range to our Enemy Unit. For those close Player Units, the Branch node will fire the True pin:
Next, we will set that close valid Player Unit as a Target:
- Connect the True pin of the Branch node to a new Set Target node
- Connect the Array Element pin of the For Each Loop to the second Target pin of the Set Target node
Finally, we will make the Try Find New Target function stop after finding the Target. Otherwise, we’ll do unnecessary computations on the rest of the PlayerUnits array. To stop this function, connect the output control flow pin of the Set Target node to a new Return Node:
Finishing Up The Enemy Unit Blueprint
You now have a basic Unreal Engine enemy AI ready to go for your RTS projects. Of course, there are more ways to expand on this concept. For example, you can add more advanced combat strategies, new types of attacks or different sorts of units, and smoother map navigation. There are a bunch of ways for you to keep improving your game here!
Regardless, though, these skills will form a good foundation you can also apply to other genres as well – only your imagination is stopping you with Unreal!
We wish you the best of luck in your future projects!
Want to learn more about strategy games in Unreal Engine? Try our complete Build an RTS Game with Unreal Engine course.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?