How do I check which instance of obj x is closest to obj y ?

0 favourites
  • 9 posts
From the Asset Store
Custom animated Health Bar - check youtube video to make it yourself
  • so i have object x, the player, and 10 instances of object y , enemies. i want, on mouse pressed, for the player to auto aim to the closest enemy. Thanks in advance !

  • The we need 2 consecutive actions, that you want to execute every time you need the player to switch target. It could be every 1 second, but maybe don't check this every tick, it could cost a lot if you have a high number of enemies.

    First action will check all enemies, and set a variable to remember what enemy is the closest. Let's call "EnemyID" (set to 0) and "EnemyDistance" (set to 1000000 to make this easier) the 2 variables :

    • For each object Y // we want to do this test for every enemy
    • EnemyDistance > distance(ObjectY,Player) // if the enemy we are checking is closer to the player than another before

    This will trigger a simple action that will set both variables, EnemyID to objectY.ID, and EnemyDistance to distance(objectY,Player).

    Once this loop is done, the variable "EnemyID" is set to the ID of the enemy that is the closest to the player. You can create a second action, that will select the enemy by it's ID using the variable, and ask the player to take it as target.

  • The we need 2 consecutive actions, that you want to execute every time you need the player to switch target. It could be every 1 second, but maybe don't check this every tick, it could cost a lot if you have a high number of enemies.

    First action will check all enemies, and set a variable to remember what enemy is the closest. Let's call "EnemyID" (set to 0) and "EnemyDistance" (set to 1000000 to make this easier) the 2 variables :

    • For each object Y // we want to do this test for every enemy
    • EnemyDistance > distance(ObjectY,Player) // if the enemy we are checking is closer to the player than another before

    This will trigger a simple action that will set both variables, EnemyID to objectY.ID, and EnemyDistance to distance(objectY,Player).

    Once this loop is done, the variable "EnemyID" is set to the ID of the enemy that is the closest to the player. You can create a second action, that will select the enemy by it's ID using the variable, and ask the player to take it as target.

    i didn't quite get the last part. would you be kind enough to reformulate? I'm new to construct

  • Something like this :

  • There is also a system condition 'Pick nearest instance'. You can use this to pick the nearest instance of Y to X and aim at it. Some calculations with distance are good to know though.

  • There is also a system condition 'Pick nearest instance'. You can use this to pick the nearest instance of Y to X and aim at it. Some calculations with distance are good to know though.

    Well... That is a lot simplier then... Lol sorry for the convoluted solution when there was an obvious one.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Guizmus yep! understanding the logic and calculating distance between objects has its own merits though

  • so i have object x, the player, and 10 instances of object y , enemies. i want, on mouse pressed, for the player to auto aim to the closest enemy. Thanks in advance !

    You just have to use the Pick nearest condition...

    Check this capx: https://www.dropbox.com/s/9q72l7hzzru3j ... .capx?dl=0

    Hope it helps. Cheers!

  • brunopalermo Guizmus thanks you all ! Can't believe the solution was right under my nose all the time...

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