How do I choose the nearest enemy which can be reached

0 favourites
  • 3 posts
From the Asset Store
Give Sound to the enemies that are part of your game! :)
  • Hi There, I am trying to create an AI for a unit. In plain words it would be:

    • Pick nearest enemy and go to him. But if you can't reach him (obstacles in the way), go to the second nearest.

    If you look at the picture attached to this post, 1 should go to A, 2 to B and 3 to C. But currently 1 doesn't move because the command „pick nearest“ picks out D. How could I exclude D from the list of possible targets?

    All the O are the same type of unit, and all the X are a different type of unit. The box which D is in might get destroyed, so it should still be targetable as soon as it is "reachable".

    For the moment I am using


    On mouse click

    System > For each X

    O > Pick Nearest to (X.x, X.y)


    Find path to (O.x, O.y)

    Move along path

    As I said, it works perfectly, as long as a path can be found to the nearest target. As soon as a path cannot be found to the nearest target, the X doesn't move.

    I would be very grateful if you could grant me some insight into a solution to this problem.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • A way you can do it I think, is to make a search bot kind of thingy like a google bot, in form of a check object. So if you add an object called "Checker". Which only purpose is to check each target to see if they are reachable and make the checker invisible, it should just run in the background.

    Then to the enemies you add a Boolean variable called "Reachable".

    As the checker goes through each enemy you changes this variable to either true or false. So if "Checker" found path to enemy you set "Reachable" to true, if it fails you set it to false.

    To your other objects, you can use the same code as you do now, but just add that it should only check enemies where "Reachable" is true.

  • Hi Nimos! Thank you for your answer, that sounds like a great idea, I will try this right now.

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