I need to make some Flying AI. Thing is… I don’t know how. I’ve made some land based AI, turrets, stationary AI, patrols, things like that. I made AI’s that used waypoints, followed a certain target, or did a certain task. I just have no idea how to make flying AI. I think flying AI is more dynamic. It has fly around like a real plane, it has to evade enemies, target them, and keep from running into objects if all possible. Unfortunately, the AI I have done has used a system where the player has a specific target (waypoint, enemy, etc…) and uses transform.LookAt, and transform.Translate to move towards the player. This system can’t be applied to flying AI.
The combat system of the planes I have worked out. Making them actually fly is… a problem, and I have no idea where to start. How do I make planes navigate in a 3D environment?
You should know a bit about my game so you can get a better grasp of what I need it to be. This isn’t going to be a flying AI that’s like a bat, a bird, or the likes. It’s going to plane… a paper plane. Nonetheless it still has to fly like a real plane. It’s a full 3D environment (or airspace, essentially).
You may want to try Unity gossip OR Unity collaboration, as those sections of the forum are more designed for general discussion an or ordering up some scripts like a number 4 at a fast food place. Airplane ai and control has been talked to death around here so try to search the forum for example scripts or projects. Plus there is a helicopter tutorial or two out there also.
Even though there are no real rules to the scripting section, for the most part people ask for help here on scripts they have already started and are needing to get fixed. Good luck, most of what you need can be found in the doc’s.
Okay then. One question I have is about evading obstacles. To make the AI evade obstacles, I was thinking of creating a Raycast that was based off of how fast the AI was going, and if the AI’s Raycast collided with an object, it would use transform.Rotate to avoid it. Is this method practical? Or is there a better way?
lol i guess that is a mickey mouse way, but no not the “normal” way of doing it or efficent way. look into A* artificial intelligence, very complicated tho
I have looked into A* Pathfinding and AI but I haven’t found any resources yet so I can learn it. Trust me, if I knew how to use A* Pathfinding, I would do it :). Any other ways or methods of doing this?
Alright. I have a pretty basic flying AI that searches for an enemy within sight range, turns to attack it, shoots it down, and then searches for a new target in range. If there’s no new target in range, it will move towards the center of the map, where inevitably, other planes will move towards. Those new planes engage and fight. They all eventually shoot each other down. If they die, they respawn.
There are three major problems with this script :
If getting shot at and doesn’t have target, plane will just move straight to eventually get shot down
When they have no target, they move to the middle of the map, but they should just wander around finding a new target
They continually crash into one another and other obstacles.
I’m not implementing my “Mickey Mouse” way of avoiding objects just yet. The second problem at the moment is by design, and I need to find another way of searching for new targets that aren’t are in range. The first one, I just have no idea how to start fixing it. I think that I should find a way for the plane to figure out whether or not it’s getting shot at, and if it is, then start moving in pre-programmed evasive maneuvers. Unfortunately, I have no idea how to program these evasive maneuvers besides using animations. I kind of have an idea how to detect whether or not when the plane is getting shot at. I’m going to work on some bugs, and then post a webplayer so everyone knows what I’m talking about.
Okay, just added a new feature to it. Unfortunately before, the speed of the plane always stayed the same. So what I did was if it has a target that was far away, it would accelerate to meets it’s speed. If the target’s distance was in attack range, it’s speed stayed constant, and if it was really close, and it was going pretty fast, it would slow down to meet it’s target’s speed. Here’s the script :
Quick question. How do I calculate/predict object’s position in the next frame? I was thinking something like object’s current position multiplied by it’s speed and divided by the FPS. If that’s the case, how do I find the current FPS?
For navigation in 3D you want to build a navmesh and use an A-Star-like algorithm or Dijkstra-like with a tuned set of heuristics for optimal path finding within your specific environment. You could also consider SLAM. You could also use a very simplistic crash and turn algorithm with repulsion and attraction fields.
For prediction, you need to research temporal integration problems or non-linear integration and linear regression. Runge-Kutta might be a good place to start.
Sorry to say it, but the problems you are trying to solve are not “easy problems” if you have never done them before. Though if you don’t care for a great solution, fudging it comes a close second.
Okay thanks. How’s my script? Good, bad, in need of work, horrible, needs optimization, etc…? If you have any confusion on what part of the script does what, I’ll be happy to explain.
I found a two fold solution to calculating trajectory.
Find the distance between the object and current object and divide it by the magnitude of the current velocity, then multiply that by the object’s velocity (this will net you a vector).
Next, you will need a maximum distance. Say 20 units for slower vehicles may be 50 for fast ones. Get a percentage of the distance between objects and see if it is less than 1. (distance/20) If it is then multiply that number by the vector above.
Now, simply add the vector to the object’s position and you get a target point to steer towards.
Very similar effects can be used for a firing solution for bullets or such.
My AI’s have a wander function, so if they don’t have a target, they just fly around. Unfortunately, they only call upon this method if target is null. Well, in the start of every match, they have a target. Once they destroy that target, the target doesn’t become null, it becomes a MissingReferenceException. That might be counted as null, but in the script it isn’t. I need to find a way to call upon the wander function if the target is a MissingReferenceException. I tried this :
But I just get an error. How do I check to see if target is not assigned and is a MissingReferenceException?
By the way, the error I got was this :
error CS0119: Expression denotes a ‘type’, where a ‘variable’, ‘value’ or ‘method group’ was expected
That’s pretty self-explanatory : MissingReferenceException is not a type, variable, or method group.
@BigMisterB When you say I need a maximum distance, do you mean a maximum distance between the two objects? In that case I already have a variable set up. And when you say…
…does that mean divide it by the player’s velocity, and then multiply it by the target’s velocity?
OK, its like this. position + Velocity * seconds is a determination of where you will be in X seconds. Velocity is in Units per second.
So, lets say that you have a generic target which is position + velocity * 10; Assuming that it will take 10 seconds to reach the position that the target will be at … in 10 seconds (which is never the case)
Now, we point our ship to go in that direction and have him travel. At the point where the ship is actually less than 10 seconds from the target, we start calculating differently.
So lets say that we are now going to measure the distance to the ship. So our distance to the target / our velocity * 10. This leaves us with a number. Either that number is above 1, because we are greater than the target’s velocity * 10 or less than because we are less than that. That number is a modifier. We only then need to clamp the modifier from 0 to 1.
So now our target is… target position * velocity * 10 * clampedModifier.
The eventuality is that the modifier will get so close to zero the closer that we get that we will intersect the enemy.
What a silly thing to say, its pretty much the standard way to do this! Its a method called steering behaviours and was explicitly created to solve issues such as flying objects (birds/boids) obstacle avoidance, maintain formations, evade, hunt, wander etc. Suggesting A* or even artificial intelligence is just plain wrong.
@NightHawx
When you get back on track to dealing with this I’d suggest reading the literature for ‘steering behaviours’ by Craig Reynolds . It requires a decent understanding of vectors math, but shouldn’t be too difficult. The pdf of the GDC99 talk is pretty good and should be all you need to get started.
Really take a glance of the pdf it deals with most of the conceptual issues you have outlined and with pretty pictures, always a bonus for understanding
However i’d be amazed if someone hasn’t already implemented these collections of behaviours in Unity, so worth searching the forums here or maybe the wiki for a pre-made solution.
…and if you want to see it in action check out the beginning of this ground breaking animation created back in 1987 using the behaviour algorithms developed by Craig Reynolds
Okay, I wish I could put up a webplayer for you guys so you can get an idea of what I’m saying (I’m too lazy to make a video), but I can’t. One of the main problems with the AI is that at one point, they all try targeting each other and they all start going in circles, as there is an endless loop. I need to figure out how to fix that…
@NoiseCrime BigMisterB - I’ll try/look into that ASAP
Sounds like the situation if both planes are targetting each other.
Off hand the way to fix that is to determine who is the attacker and who should flee. One of the planes should have some advantage over the other, i.e. they are already behind their target, meaning the other plane is in front and at a disadvantage. That way the attacker can use its ‘follow’ behaviour (i.e follow, attempting to minimising distance to target), whilst the other should ‘flee’ (i.e Maximise its distance from the attacker).