Find Nearest

0 favourites
  • 6 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I've seen this pop up quite a few times and read up on quite a few examples.

    The problem is I can't see what I'm doing wrong with this. The Player sprite is part of the Players family... but it's not working the way I imagine. I'm only using 1 sprite for all the players.

    Instead of setting the "ClosestEnemy" variable to the number assigned to the closest enemy, it appears to be setting it to the enemy with the lowest number.

    I've seen people do similar with more events, and I myself have done so, but I'd rather not go through the hassle of complicated code when I know it can be done with only a few lines.

    I imagine I'm close, but I can't quite get it right.

    Can anyone help?

  • It's kind of hard to tell what you want to do.

    Family pick closest to player.x, player.y perhaps?

    I'd get rid of the inverted, and for each.

    Or maybe just player pick closest to family.x, family.y

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I wouldn't say it was close, I'd say it was correct. Not sure why it's not working, pick nearest but not self? Works fine for me.

  • newt Yeah, that's pretty much what I'm trying to do. I have 4 of the same objects, all a part of the same family. I want each one to find the nearest other version and assign it as a target.

    As I said, I've done it a few times in the past, but they were somewhat convoluted. I saw this example on this forum, and thought I'd give it a go, but can't see what I'm missing.

    I can post the capx I'm working on, but one needs the Chipmunk behaviour to get it to work. Will post the capx shortly.

    Nevermind, solved it. I had a line above that which was only looking for a certain variable, and it was throwing everything off.

  • Speaking of lines...

    Was just about to suggest the line of sight behavior.

  • Try this:

    var tempUID = sprite.UID

    var tempX = sprite.X

    var tempY = sprite.Y

    C:Pick by comparison: Object: sprite, Expression: sprite.UID, Comparison: ≠, value: tempUID

    C: Pick nearest sprite to tempX, tempY

    It creates a SOL of all sprite objects that do not share the UID you wish to avoid picking, then it picks the closest one of those to the player sprite's position.

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