How would you create a enemy/ally plane AI so that it takes off and lands, while flying it finds targets on the ground and in the air. Also when another plane is attacking it, it has to try to not get hit. When attacking an enemy on the ground it has to angle/fly towards the enemy and shot but then pull up when the plane is to close to a object (buildings/ground).
Thanks
Hehe…that’s a huge question!
You’re asking how to do about 10 different fairly complex things, so it’s not really something someone’s going to be able to answer comprehensively in a forum post. (Well, they could I suppose, but it’d be a lot of work ![]()
What you’ll probably need to do is break the problem down into separate parts, and then try to solve those parts individually. Some of the problems you’ll need to solve include:
- How to make the plane ‘fly at’ a target.
Depending on how realistic you want to be, this could be as simple as using the ‘look at’ transform function.
- How to avoid crashing into stuff.
This will probably involve checking for impending collisions (or maybe just checking the current altitude), and changing the current AI state (which I’ll address later) to ‘pull up’ if necessary.
- How to evade incoming fire.
This could be fairly simple, or more or less arbitrarily complex. One evasive maneuver that would probably be reasonably straightforward to implement would be to steer away from the enemy’s line of fire.
- How to locate targets.
Could be done by querying for nearby objects using the physics system. If that’s prohibitive due to the ranges that would be involved, you might need to do some sort of search through a ‘database’ of potential targets occasionally to find targets that are nearby.
- How to take off.
Should be pretty straightforward.
- How to land.
A little more complicated, as it involves navigating back to the runway, getting properly lined up, decreasing altitude, and so on.
All of the different things you want the AI agent to do should (ideally) be programmed individually and tested in isolation to make sure they work the way you want them to. Then, typically, you would tie them all together using a finite state machine.
Basically, with an FSM, the agent is in a single state at any one time. In this case, states might include taking off, landing, searching for targets, attacking, evading, and avoiding a collision.
Each update, you take the appropriate action based on the current state. That action may in some cases involve switching to another state; for example, detecting low altitude would cause a switch to the ‘pull up’ state, being fired on would cause a switch to the ‘evade’ state, and running low on fuel would cause a switch to the ‘land’ state.
There are a lot of different ways this could be implemented, but the above should at least give you an overview of what problems will need to be solved.