How do I compare distance to another object

Get help using Construct 2

Post » Fri Feb 06, 2015 9:35 pm

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.
B
10
S
5
G
2
Posts: 37
Reputation: 1,992

Post » Fri Feb 06, 2015 9:44 pm

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.
B
38
S
12
G
11
Posts: 331
Reputation: 7,712

Post » Sat Feb 07, 2015 12:46 am

I owe you another thank you frag thank you :D
B
10
S
5
G
2
Posts: 37
Reputation: 1,992

Post » Sat Feb 07, 2015 1:55 am

Seems to be saying syntax error for the first less than bracket in that code :( I cant figure out how to fix it lol
B
10
S
5
G
2
Posts: 37
Reputation: 1,992

Post » Sat Feb 07, 2015 5:04 am

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
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Sat Feb 07, 2015 5:16 am

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.
B
10
S
5
G
2
Posts: 37
Reputation: 1,992

Post » Sat Feb 07, 2015 6:22 am

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.
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Sat Feb 07, 2015 6:24 am

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)
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Sat Feb 07, 2015 8:32 am

haha yes I did, thank you for that sgn15 greatly appreciated :D and thanks for the last tip I was wondering that.
B
10
S
5
G
2
Posts: 37
Reputation: 1,992


Return to How do I....?

Who is online

Users browsing this forum: kabaal, oddreal, raulcg1981 and 10 guests