Tactically Intelligent AI

0 favourites
  • 5 posts
From the Asset Store
Units do not overlap each other and use different ways if there are several free ways.
  • Hi! I'm working on a top down shooter game and I'm struggling to make an A.I system that makes me feel like I'm fighting another player that has a remote idea of what they're doing.

    In my game, enemies roam around the map until they see the player or hear them. After hearing or seeing them, they will move to the appropriate engagement distance(Distances of engagement depend on what gun the opponent has) and attack. At the moment they just sit in one spot and shoot until they kill the player or get killed themselves.

    This doesn't quite meet my needs for the AI, I want them to be able to recognize cover and hide behind it if they get hurt. Or have the ability to make a path that flanks the player.

    I have a general idea of how to code the hiding behind cover part, just move to a position where the players line of sight is obstructed, after all...you can't shoot what you can't see. But how to find an area where the players LoS is obstructed is something I'm not sure how to do.

    And I don't even know where to start with the flanking part...

    I know this may be very difficult to answer in one post, but any help or advice that gets the ball rolling would be GREATLY appreciated!

  • This is an interesting problem. I'm sure there are already algorithms out there, but I'm going to take a shot in the dark.

    Taking cover is only applicable from the player's point of view. So when triggered, I would cast line of sight rays from the player towards the enemy, starting from a direct line, and then gradually getting wider to the sides. When an obstacle in range is hit, create helper "movement targets" along the path of the ray away from the player starting from the intersection point, deleting ones that overlap, until the first free space is "hit". That will give you a bunch (or just 2, or only first valid one, really up to you how long to search) of valid points that are behind cover from the player. Then we'll want to pick the one to path find to, either the closest one to the player, closest to the center line, closest to the enemy, ect. We can add additional conditions to invalidate movement targets, like too close to the enemy, too high pathfinding cost, ect.

    Upon reaching cover, I'd imagine the enemy would need to pop out to get los to shoot, which would simply be pathfinding to the player location until line of sight is acquired, stop, and shoot.

    Flanking is similar, except target the rays to the side of the player instead. Build a list of potential movement targets, and use the one that makes most sense, probably the closest one to the enemy unit. You can use additional invisible helper sprites on either side of the player to check for valid "flanking spots", if the movement target overlaps these helper sprites, then it is a valid target. Otherwise, not.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here is my idea, somewhat similar to oosyrag's.

    When an enemy needs to move - randomly generate a bunch of destination points in the walking distance and in general direction to the player. You can spawn invisible sprites at each point to make it easier. Then evaluate each point - is it behind a cover, does it have a line-of-sight to the player, is it close to other enemies etc. You can give it a score, say +5 if it's behind a cover, -2 if it's close to other enemies (because you want them to flank the player). The point with the highest score wins and the enemy moves there.

  • Thank you for your reply..

    It's worth noting that I'm fairly new to Construct 3 so I'm not sure how to do some of these things.

    I managed to get objects to spawn in the vicinity of the enemy with

    "System: Create object TestHelper on layer 6 at (Enemy.X + random(-1000, 1000), Enemy.Y + random(-1000, 1000))"

    But I'm not sure how to have the enemy move to a specific instance out of the 10 that I spawned.

    I'm sure I could figure out a scoring system for these points, but any advice on how to effectively code the position scoring would be greatly appreaciated..as you'd probably code a more robust system then I would.

  • You can use LineOfSight behavior (or multiple LineOfSight behaviors with different settings) on the TesterHelp sprites to check if the player is in LOS, or if there are other enemies nearby. It also allows to cast rays, for example if you need to find the exact position of an obstacle between that sprite and the player.

    I also suggest re-using these TesterHelper sprites, instead of constantly creating and destroying them. Keep them somewhere off-screen when they are not needed, and move to the random point when required.

    This whole task is pretty advanced. I recommend studying a few tutorials and built-in examples in C3 on how to use LOS behavior and raycasting. Try to make a very dumb AI first, then improve it bit by bit.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)