I saw some reddit post asking how to do a simple flying AI. People respond with octrees and stuff like this but it sounds like something flappy-bird level of simplicity is required.
Here is how I’ve done some simple 3d flying enemies in my project. Showcase of what they look like in video below.
Much of this code is specific to my AI but the basic movement you can see how it works:
Where to go?
POI actors placed in the level:
How to get there:
It works like this:
I place some locators in the level. These report there distance to the player to a manager class.
If they are close enough, the manager adds them to an array. This is the “active POI’s”.
These are possible locations the flying enemy can move to.
Now the code just cycles between moving to a POI and then shooting.
What about obstacles?
Well a little creativity can solve that problem. In my case I just put the POI’s strategically so there basically is no obstacles. Rarely they might fly under a roof, but since we are using the impulse to move them with physics, the AI keeps moving towards destination and just slides off of the obstalce. It happens rarely and doesn’t look very strange, you see stuff like this even in AAA games.
Anyway, that’s it! Just set a location and add impulse in that direction, and measure distance to know when they arrive.
You can see how it works in action here (more flying AI seen near end of video)