How do I compare distance to another object

0 favourites
  • 9 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hi,

    Im trying to compare distance between player and an object, so a skill can only be activated when they come within a certain distance. any help would be great thanks.

  • There is a distance function that takes the x, y value of the first object followed by the x, y value of the second and returns the distance. Use the system's compare two values event and type:

    distance(<name of object 1>.X, <name of object 1>.Y, <name of object 2>.X, <name of object 2>.Y)

    in the first value and the desired distance in the second.

  • I owe you another thank you frag thank you

  • Seems to be saying syntax error for the first less than bracket in that code I cant figure out how to fix it lol

  • post your expression here so we can see the syntax error

    Also, if you only want the horizontal distance, you can use a distance variable that saves the difference of player.x and object.x then use that variable to check if the distance is within that value you want

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • thanks sgn15, ill try that, but as for the error, I was referring to frags code he has there, c2 doesn't like the less than bracket at the start.

  • What FragFather meant was to:

    replace everything inside the < > including both signs (less than and greater than) with your object

    <name of object 1>.X,

    <name of object 1>.Y

    <name of object 2>.X,

    <name of object 2>

    so it should be:

    player.x

    player.y

    object.x

    object.y

    You just misunderstood him.

  • Oh, about the variable thing I suggested, use abs(player.x - object.x) for the variable. Then if you want to check if the object is to your (player's) left or right, use Compare X to check if object is at your left or right. (greater than means to your right, less than to your left)

  • haha yes I did, thank you for that sgn15 greatly appreciated and thanks for the last tip I was wondering that.

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