Enemy AI?

0 favourites
  • 12 posts
From the Asset Store
Enemy turtles pack including fifteen fully animated variations
  • hi, I searched the tuts for this but didn't find much on it..searching for "enemy" or "enemy AI"

    can anyone point me to a tutorial or give advice on setting up an enemy..like in Asteroids.. a spaceship comes on the screen and goes after the player? Shoots weapons with varying degrees of accuracy, maneuvering patterns and has self preservation by not flying into objects that would hurt it.

    this is probably way too much for a tut, but something to get me started would be helpful.

    I created an enemy sprite, gave it wrap, turret and bullet behavior.. I have it acquiring the player as a target when it's within a certain range. I can't get it to fire on the player and it ends up running into the player and staying on top of him...I know I should change direction within a certain range..but I'm not sure I'm going about this the right way. I would like to make the enemy a challenge to the player..not a boss, but not just something to shoot and move on. Could use some advice..


  • My first ever prototype made with C2 is called Icarus Wave Attack. You can play it here.

    The AI behavior isn't perfect, but you'll notice the AI ships will tend to strafe around the player, occasionally turn to shoot asteroids that are close, and more.

    I will give a basic overview of the behavior I created. It uses a combination of custom movement and pathfinding.

    There are two major questions:

    -Is the AI ship near the player ship?

    -Is the AI ship near an asteroid?

    The basic case, which is the easiest, is when the AI ship is not near anything. Basically I have a variable called 'closeRange' and if the AI ship is not within closeRange to the player ship AND not within closeRange to an asteroid, the pathfinding behavior is activated and the ship finds a path to the player ship and moves.

    Another case is when the AI ship is not near the player ship but near an asteroid. In other words, when distance(AI ship, player ship) > closeRange but distance(AI ship, asteroid) < closeRange, then have the player ship STOP pathfinding and turn on custom movement to move away from the asteroid. I also use the turret behavior to have the AI ship shoot the asteroid.

    Another case is when the AI ship is near both asteroid and player ship. Basically this is when both checks in the section above are true. In this case, I have the AI ship prioritize its own survival and accelerate (custom movement) away from BOTH the asteroid and the player ship. Using a couple of condition checks and the turret behavior, the AI ship will fire at the closest object (which will be either the player ship or the asteroid).

    At any point in the game, the AI ship is close to the player but not an asteroid, close to an asteroid but not the player, not close to anything, or close to both.

    Hope this helps. Note that AI is one of the hardest aspects of game development to accomplish well. If you're just starting out in C2, I recommend starting with just something basic, such as an AI that chases the player but also crashes into asteroids. In development, many things are often more complex than they seem at first, and this is especially true for AI.

  • this does help thanks... love your game btw... similar to what I am trying to make...which is basically a way for me to teach myself construct2.

    yeah AI is a whole thing unto itself... I've written (top down) programs in the past that act like ai, it's very hard to make convincing and for users not to immediately see a pattern. so many checks, so many different potential situations. I don't care about realism as much as just making a challenge for the player.

    I've never used familys or containers in C2.. is that how you set up your enemies? seems like I have to do so much nested checking. My AI ships are going to be a random occurrence and not always on the screen (like asteroids).

    One of my main road blocks is syntax and structures of what you can do in C2... i.e. if then, else, while true, case, nested statements etc.. I will learn by doing and have already learned loads just wish there was a good jumping off tutorial that would help jump start me.    

    Like is it inefficient to keep checking things that are not even going on? should I be checking AI ship things even when one isnt on the screen? or do I always include ->'object' on screen in all my checks? seems tedious coding. Is there a better way? like set an AI-ship flag=On and then execute that code.. seems like you can't do much nesting in C2? I suppose I could create a Group and deactivate the group? until the AI-ship is spawned? is that a better way?

  • Unless you're using physics, which uses up a lot of CPU processing power, the performance gain from not checking AI conditions when there are no AI ships on the screen is marginal. However, for good practice, it's probably best to disable the AI ship behavior group if AI_ships.count = 0 and then enable it if AI_ships.count > 0

    I use families for checking. This makes it easy to expand upon. For example, in the future I may add 'allied' ships to the game. Instead of adding condition checks for these allied ships, why not put them all in a family and check if the AI ship is near the family? Same goes for adding different asteroid sprites. Put them in a family and do the condition check on the family.

  • thanks, good to know... I will look into using families since I may expand to different ships as well.

  • still working on this.. thanks for the info! you should make a tutorial!

  • I probably could once I have free time.

    It's honestly more complicated than what I posted. There is A LOT to account for, and even throwing together a basic .capx showing off this type of behavior would be a lot of work.

  • There was a good post about AI in Castle Story developers' blog, although that might be basic stuff for you: sauropodstudio.com/dev-diary-number-fifteen-ai-basics

  • There was a good post about AI in Castle Story developers' blog

    cool, thanks for this..

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Excal yeah, it is VERY complicated.. I'm having some trouble.

    There seems to be more than 1 way to do it. Or at least more than 1 way to attempt to do it...(as I have not been successful yet)

    I am only having 1 AIship to battle, so I am trying some "evasive" patterns with the AIship. It's proving difficult especially on collision with the player... it get's jittery causing mulitple collisions. I've built in some work-arounds, but it's not easy!

  • Just putting this link here for interesting reading:


  • I'll try get around to a tutorial at some point, and I should post when it's not midnight and I'm burned out from too much coffee but the basic process I used for schmup was to create a family with basic behaviours, like follow player if line of sight and on screen, fire if the player falls within a certain angle etc, then elaborated for sub classes, like the snake/turret things, or the claw monsters that fly towards you and spin.

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