I’ve been working at a system for aircraft/missiles to seek the player (in a jet), but I’ve got one problem - obstacles. It looks pretty stupid if an enemy high-speed jet that’s supposed to be following you smashes into a high-rise building because you turned around it.
I’m using raycasts to detect objects, but I can’t work out how to get the enemy to avoid them.
One option that I tried (which failed monumentally) was just dodging in the same direction, the same amount every time. (If it sees object, move to a position 5 meters higher until it’s passed).
The biggest problem is that the terrain has large amounts of formations that reach into the sky (eg. rock arches, high-level bridges), so if the enemy always dodges up, he’ll end up hitting something, and if he dodges down he’ll eventually crash as well.
So, to put it very concisely:
How can the enemy always dodge objects in the right direction?
Would a set of rays work? I’ve been thinking :
Why not use five raycasts - four on either side (top, bottom, left, right) used to check each side, and one to check ahead. The 'side’s rays could only turn on when the front one sees something. Then the ship could turn to avoid hitting a wall.
Only one question : how to turn? Quaternions are hard…
You have dipped your toe into AI and are probably realising very quickly why its so hard. I am doing a game with ships in 3D atm and navigation is a toughie. You can handle it a number of ways:
Potential fields - each object is given a field of influence and when a moving object comes near it the moving object exhibits some kind of behaviour. In your case you’d want steering behaviour and the closer you are the steeper the turn. Stay away from hard-coding conditions like your first solution as they greatly reduce what an agent can do and you’ll spend more time gearing the world because of them.
A raycast: these aren’t very good as they only check points and objects that are large will make them almost useless. How large is the object? How is it shaped? You could try turning based on distance and object type but you might also miss with a raycast because of how the object is shaped.
Way points: these are rigid and you’ll have to implement some kind of smoothing and hand-place them according to all your agents’ speeds, turning speeds etc. Imagine if a large enemy flew to a waypoint that was near enough to a building for a small enemy to pass it but a large enemy would get stuck
Nav mesh: I haven’t much experience or knowledge on 3D nav meshes but they’d be worth looking into
Keep it 2D and use a path-planner like Alex’s ( Simple Path, I think ) on the asset store or mix it up with your own 3D code or animations.
Agent navigation is a difficult issue. I’ve decided to use a list of statics and potentials fields coupled with steering behaviour for the unchanging aspects of the world and a mix of waypoints and steering for safe-harbour type locations. You’ll have to restrict some motion if you’re using Rotate… whatever because it makes the aircraft move unrealistically sometimes.
Is this for a mobile game or PC? The hardware power will restrict you greatly in what you can do. How many agents will be on screen at any time? If its just you and one enemy and a bunch of statics then it’ll be straightforward but if there are other agents on-screen at the same time it becomes more difficult then.
Thanks for replying!
To answer the questions stated in the last paragraph:
It’s for PCs, and there’ll be a maximum of 5-6 enemies visible at a time, probably 2-3.
I’ve experimented with a linecast and waypoints, to either seek the player or patrol, which seems to work well.
I plan on using two raycasts, one on either side of the ship, to determine which way to turn to avoid objects.
Having read all of your methods, they seem to have both plus and minus sides. My decision on what to use I think works best in my game’s enviroment.