Featured Blog

Artificial Agent behaviors for Choosing/Switching Targets , Calculating Hit Chance

How an artificial agents should choose/switch target? How should I calculate threat of the enemy? How should you calculate the hit chance and decide if the target has shot or not? Here's one way of how.

A few months ago, I was searching through web about how artificial agents should switch target?, How should I calculate threat of the enemy? How should I calculate the hit chance and decide if the target has shot or not? and I’ve found almost “nothing”. So I decided to write some formulas according to my experiences in games and some real world situations.

I want to share them with you so that if you’re currently searching for same kind of thing you can find it right here!..

First lets look into threat calculation

Threat value is calculated for each soldier in the list of visible enemies by using factors below:
Distance to target
Angle difference between the defensive soldier and the enemy
The angle of the heading of the enemy and whether the enemy is firing or not
These factors are given different coefficients to balance the effectiveness of these factors in the threat level. So you can change the coefficients as you see fit.

Final formula generated by using these factors is

threat formula

  • DN is the distance normalized with maximum visibility distance.
  • If the enemy heading to the defensive soldier is lower than a minimum angle, A is 1,
  • otherwise it is 0.
  • If the enemy is firing, F is a constant value, otherwise it is 0.

Once you calculate the threat value for each seen enemy in the list, the enemy with the maximum threat can be chosen as the current target.

Lets get to the Hit Chance Calculation

To check if a shot is successful, a calculation is made based on the target distance, aim accuracy, the health of the soldier, whether the soldier is under fire or not and an additional random chance modifier.

With these bear in mind, the final formula is like this


  • DN is the distance normalized with maximum visibility distance.
  • AN is the angle difference between the aim of the defensive soldier and the enemy normalized
  • with minimum firing angle.
  • Ccoef is a coefficient depending on the cover status of the target.
  • If the soldier is under fire, U is a constant, otherwise it is 0.
  • Rnd is a random chance modifier.
  • chanceModifier is an external modifier that changes with incoming shots and the type of shooting being made (between 0 and 1)

If Hit Chance is above a certain value the shot is successful.

I think these formulas are simple and more than enough to calculate threat and hit chance for most of the artificial agent scripts. 

Hope these were usefull..till then.. For more..

Burkay Ozdemir
Deniz Aladagli 

Latest Jobs

Cryptic Studios

Senior Producer

Anne Arundel Community College

Arnold, MD, USA
Instructor/Assistant Professor, Game Art

Night School Studio

Los Angeles, CA, USA
Level Designer / Scripter, Games Studio
More Jobs   


Explore the
Subscribe to
Follow us

Game Developer Job Board

Game Developer Newsletter


Explore the

Game Developer Job Board

Browse open positions across the game industry or recruit new talent for your studio

Subscribe to

Game Developer Newsletter

Get daily Game Developer top stories every morning straight into your inbox

Follow us


Follow us @gamedevdotcom to stay up-to-date with the latest news & insider information about events & more