How do units choose which enemy to target in SC2?

Solution 1:

In addition to Tzenes' answer, there are a couple things to note:

  • Your units will preferentially attack enemy units that are attacking them. If there are two enemy units in range and only one is attacking, the attacking one will be targeted.
  • Your units with instant attacks will not overkill. This has been most frequently noted with tanks. If you have a zergling that wanders in front of five tanks, only one will target him. Units with non-instant attacks, like missiles, can still overkill.
  • As illustrated by Tzenes' ATP/OTP tables, the AI will always pick other units in range over enemy workers.

(Additional discussion here)

Solution 2:

So attack priority is based on two factors: ATP and OTP. ATP is used by default, with OTP used as a back up if other units are close. Higher # = Higher priority. During beta units with an OTP of 0 were calculated the same as other OTP numbers. This changed in one of the patches to have the OTP of 0 mean use atp value.

This list can also be found on Liquidpedia

Unit            ATP   OTP

Protoss

Archon          20    0
Carrier         20    0
Colossus        20    0
Dark Templar    20    0
High Templar    20    0
Immortal        20    0
Intercepter     20    0
Mothership      20    0
Observer        20    0
Phoenix         20    0
Probe           20    10
Sentry          20    0
Stalker         20    0
Void Ray        20    0
Warp Prism      20    0 (identical for both modes)
Zealot          20    0 
Force Field     0
Forge           11
Fleet Beacon    11
Gateway         11
Cybernetics     11
Dark Shrine     11
Nexus           11
Photon Cannon   20
Pylon           11
Robotics Bay    11
Robotics Fac    11
Stargate        11
Archives        11
Twilight        11
Warpgate        11 

Terran

Auto-Turret     20    0
Banshee         20    0
Battlecruiser   20    0
Ghost           20    0
Hellion         20    0
Marauder        20    0
Marine          20    0
Medivac         20    0
MULE            20    10
Point Defense   20    0
Raven           20    0
Reaper          20    0
SCV             20    10
Siege Tank      20    0 (both modes)
Thor            20    0
Viking          20    0 (both modes)
Armory          11
Barracks        11
Bunker          19
Command Center  11
Engineering     11
Factory         11
Fusion Core     11
GhostAcademy    11
MissileTurret   19
OC              11
Planetary       20
Reactor         11
Starport        11
Supply Depot    11
Tech Lab        11 

Note: Flying does not affect priority

Zerg

Baneling        20    0
Brood Lord      20    0
BL Cacoon       10    0
Broodling       20    0
Changeling      20    0
Corruptor       20    0
Creep Tumor     11    0
Creep Tumor     19    0 (Built)
Drone           20    10
Hydralisk       20    0
Infested        20    0
Infestor        20    0
Larvae          10    0
Mutalisk        20    0
Nydus Worm      11    0
Overlord        20    0
Overseer        20    0
Overseer        10    0 (Cocoon)
Queen           20    0
Roach           20    0
Ultralisk       20    0
Zergling        20    0
Baneling Nest   11
Evolution       11
Extractor       11
Greater Spire   11
Hatchery        11
Hive            11
Hydralisk Den   11
Infestation Pit 11
Lair            11
Nydus Network   11
Roach Warren    11
Spawning Pool   11
Spine Crawler   20
Spine Crawler   19  (Uprooted)
Spire           11
Spore Crawler   19
Spore Crawler   19  (Uprooted)
Ultralisk Cavern11 

note: Burrowing does not affect priority