Basically, by staying aware of what’s going on. They work as part of a large team. Rules of engagement (when you shoot) are going to vary a lot depending on the situation.
So, e.g. in peacetime they’re going to rely on visual identification. Can’t go around blowing up every civilian who forgot to turn their transponder on.
In wartime, dedicated radar planes are going to try very hard to have a comprehensive picture of everything in the air. If something took off from a hostile airfield and is zooming towards your planes, you can draw some conclusions.
Warplanes are up there as part of a plan. There’s something called an Air Tasking Order and probably a lot more that plans all the air missions for each day. Pilots should have a good idea where all their friends are.
The tech stuff-radars that can id planes and IFF systems, etc., are, I think, secondary, with the big exception that a friendly IFF is probably an near-absolute don’t shoot. But I think if it gets to that point, a lot of things have already failed.
[Here’s](https://theaviationgeekclub.com/eagle-vs-foxbat-when-usaf-f-15s-dogfighted-with-iraqi-or-russian-mig-25s-during-operation-desert-storm/) a first person account of what starts as a beyond visual range engagement that turns into a dogfight.
Latest Answers