How to create temporary danger zones on ground and have ai characters move from them?

Hi!

I’m making a small strategy game where I want ai controlled characters to move from danger. Every now and then I want incoming danger (e.g. mortar rounds) heading for one or more random areas of the ground. I want these areas highlighted only while danger is incoming and ai characters inside the areas to recognize that they are within the areas and to move out from them.

Something like:

  • launch five missiles
  • for each missile create a random impact zone
  • while airborne missile highlight impact zone
  • for each ai character if character inside impact zone move character out of danger zone
  • after impact delete danger zone

Well, how are you doing your AI now?

If it were me, I’d probably use a utility-based AI, which would make this easy: you simply keep a list of danger zones, and getting out of danger is one of the (high-priority) urges the AIs have.

But if you’re using some other approach, you can do basically the same thing, in whatever way that approach does it. For example, if you’re using a state machine, then you just need to have something in most states that checks the danger-zone list, and if they’re in danger, switch to a fleeing state.

1 Like

I’d recommend some kind of behavior state machine, like @JoeStrout said with his second option. The way I see it working, your incoming mortar indicator would spawn a cylinder trigger collider along with it. Any object that needs to react (ground units) should have a component on them that has an OnTriggerEnter method defined. That method should set a flag in your state machine to have them flee as opposed to their normal move/attack state.

Depending on how realistically you want the units to act and react, you could spawn another (larger) trigger once the round impacts. Any unit inside this radius that isn’t killed would switch to an explosion-react state where they dive to the ground if they’re moving or hunker down if they’re stationary. After their appropriate animation finishes, they can go back to their normal state.

How the units flee the impact zone is another matter. Realistically, there’s no way to tell where the precise impact point is when you hear a mortar round incoming. (Well, realistically, there’s no way to outrun an incoming mortar… you’d just try to get behind/under cover if you hear one) People would probably pick a semi-random direction to run in (with a preference towards objects that may protect them, or even other units as your survival instinct may suggest their bodies could shield you), but they’d also tend to flock together a bit. The best way to simulate that would be to detect nearby objects and units to the impact zone and find a normalized point within the impact zone that lies close to their location. The point from which units would flee would be the inverse of that point.

So for example, you have a blast radius that lies directly west of a “bunker” object, with a small group of trees to the east. Assuming the points of the impact zone have (-1,-1) at the SW corner and (1,1) at the NE, the closest point of desirable movement would be something like (0,0.75), with the fleeing point being (0,-0.75). Units would spread out from that point, with ones really close to the trees running for them, while everyone else more or less makes for the bunker (kinda, since objects at the N and S ends of the impact zone would actually run more or less perpendicular to it, but they’re still getting away from the mortar round).

Of course, if this is all a bit more complex than you cared to get, you could just select the fleeing point at random. It’ll have the unfortunate side-effect that if the fleeing point is at (-1,0) and a unit is standing slightly to the east of that, he’s probably not going to make it out alive, since he’ll foolishly run through the entire impact zone instead of moving a few feet to the west. So if you care more about troops surviving (to avoid frustrating the player or making mortars too overpowered), I’d recommend just offsetting the center of the impact zone by 0.25 or so in a random direction and have units run from that.

1 Like

Many thanks for the replies. I found them very interesting, particularly the ideas about how the units should move from danger zones.

I think that going behaviour state is a good idea, and that I may be able to do that (albeit in an inefficient, amateurish way).

However, it is still unknown to me excactly how to go about creating and marking the impact zones.

I would also greatly appreciate some tips regarding how ai could decide on best path out of danger zone, where they e.g. would be more likely to chose a path that would keep them within a certain distance of a target unit or object (without having them always move directly towards the target, they could move in any direction, chosen based on stats and rng).

If someone could point me to learning material, such as tutorials, that directly deals with subjects at least closely related to my challenges or could post some simple c# code I could start tinkering with I’d greatly appreciate it.

I tend to like influence maps for things like this. Basically you have an array of some sort representing the area. Then when you add danger to an area you simply increase the numbers in the danger area. If you integrate this sort of approach with your pathfinding cost, then your entities will automatically avoid danger without ever needing to be explicitly directed away.

Influence maps are a deep rabbit hole, its easy to get carried away making a map for everything.

Honestly, the subject is too big and to high level to paste relevant code here. A complete AI solution has a lot of moving parts. Plus AI almost always ends up being game dependent. The best option I can point you at is M Buckland’s Programing Game AI by Example.

2 Likes

Thank you, BoredMormon.

Now, if we leave the subject of AI aside.

I would expect it to be fairly straightforward and easy to create a circle on a ground plane at a random location a certain distance from a known position (e.g. from the position of a mortar). Am I wrong in assuming this?

I am thinking that if that can do that for the impact zones I could use a similar method to calculate possible random target destination positions for the characters from which the characters could semirandomly chose one target destination.