How do I compare distances between multiple instances?

0 favourites
  • 5 posts
From the Asset Store
Be quick and choose the right answer for the shown equation.
  • I have a map with 9 spawnpoints, and 3 enemies. When i die i want to spawn at any of those points based on the distance to those enemies. Preferably the safest one furthest away from any of these. I know how to get the distance between a spawnpoint and an enemy, but only for one instance of each.

    for example.

    for each spawnpoint

    distance(spawnpoint.x, spawnpoint.y, enemy.x, enemy.y) - will only get me the distance to one if the enemies. or is there any way to specify which of the enemy instances i want the distance to?

    how do i get the distance between all of them?

  • you can use the "nearest/furthest" action in the spawn point object.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Locale variable "CurrentBiggestDistance"

    Locale variable "MostFarUID"

    OnDeath

    ------for each spawnpoint

    -------------for each enemy

    ------------------------distance(spawnpoint.x, spawnpoint.y, enemy.x, enemy.y) > CurrentBiggestDistance : Set CurrentBiggestDistance to distance(spawnpoint.x, spawnpoint.y, enemy.x, enemy.y) AND set MostFarUID to enemy.UID

    -------Pick spawnpoint UID= MostFarUID : Spawn Player

    -------[No condition] : Set CurrentBiggestDistance to 0 AND Set MostFarUID to -1

    gl!

  • Locale variable "CurrentBiggestDistance"

    Locale variable "MostFarUID"

    OnDeath

    ------for each spawnpoint

    -------------for each enemy

    ------------------------distance(spawnpoint.x, spawnpoint.y, enemy.x, enemy.y) > CurrentBiggestDistance : Set CurrentBiggestDistance to distance(spawnpoint.x, spawnpoint.y, enemy.x, enemy.y) AND set MostFarUID to enemy.UID

    -------Pick spawnpoint UID= MostFarUID : Spawn Player

    -------[No condition] : Set CurrentBiggestDistance to 0 AND Set MostFarUID to -1

    gl!

    you can use the "nearest/furthest" action in the spawn point object.

    Thanks both. i managed to make crossover between both solutions, marking any spawnpoint with no enemies within a range as safe, then spawning at a random "safe" spawnpoint.

  • You are welcome

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