Aiming at the opponent
Before you can go through this example you need to first setup your environment. You can find out how to do that in our Getting started guide .
In this example you will learn how to make your warrior units aim at the opponent. At the end we will also give you a few extra tips that will improve your warriors aiming mechanics drastically!
This is how our units will be able to aim:
Code
Below is the code for this example.
⚠️ To keep the example clean we didn’t include the whole code for the bot. In order to use this code you need to paste it to an appropriate location in your bot implementation.
|
|
|
|
|
|
Explanation
In order to aim at the opponent we need to make warrior unit turn towards it.
Deciding about where to turn is achieved by calculating the angle between the warrior’s orientation and the opponent as show in the image below.
With every GameState you receive orientationAngle
for each unit.
The aiming angle
is the angle from unit’s orientation towards the point.
If the aiming angle
is 0
the unit is looking directly towards the opponent, if it is positive the unit is looking to the right side of the opponent and if it is negative it is looking towards the left side of the opponent.
When we calculate the aiming angle we just need to make our warrior turn towards the direction that will decrease the size of the aiming angle.
Library MathUtil has a couple of helper functions that you can use for quicker bot development.
♖ Extra tips
Better aiming - Although the warrior can turn towards the opponent it can’t aim properly and misses him very often. You can easily fix that by adjusting the rotation speed when the aim angle gets smaller. For example when the angle is smaller than 15 degrees don’t rotate with
RIGHT
orLEFT
but withSLOW_RIGHT
andSLOW_LEFT
rotations.Following opponent - When the aim angle is small enough you can make your warrior move forward with Api command
setSpeed(int unitId, Speed speed)
. This will automatically make it follow the opponent when it tries to flee. But be careful, your warrior units now go close to the opponents when they fight them and will thus be often killed by their teammates that are aiming at the same opponent. You will need to implement some team kill prevention logic to fix that! MathUtil will come in handy.Choose your target - If your warrior has more then one opponent in it’s viewing area, shoot at the one with the least amount of health or pick some other interesting strategy.
Next up
In the next example we will improve the logic for spawning our units. It is very important that we have a good balance between how many worker and how many warrior units we spawn and have on the map at the same time. Follow the link below to learn more.
Next: Smartly spawn your units