This example shows how you can improve the way enemies aim at the player in your games. I made this because a lot of games use the "dumb" method, which is pretty rubbish. Here's an overview of the 3 layouts in this file:
Layout 1: "Dumb aim": just always set angle towards player, and shoot regularly. By the time the bullet arrives, though, you'll have moved somewhere else, so it's easy to avoid fire. A lot of games do this, and you can just skip straight through heavy enemy fire by keeping moving!
Layout 2: "Approximate aim": work out how long the bullet will take to arrive, then aim at where the player should be after that amount of time. Good, but not perfect - if the player moves further or closer away, the time of arrival changes, then the bullet misses. So some paths you run still mean enemy fire lags behind you. Not good enough!
Layout 3: Perfect aim. Solves the equations to work out precisely where to aim the gun such that when the bullet arrives it intersects exactly with the player. If you run in a straight line, the enemy fire will hit you with 100% accuracy. The only way to avoid is to change speed or direction. Much more realistic and challenging! No more plain sailing through enemy ranks!
The maths is provided in a comment in layout 3 and is pretty hardcore (I worked most of it out myself
) so you've been warned. I think I might make this a built-in expression or something, because it's very useful for gameplay and the formula is pretty complex.