# How do I get the distance between 2 objects based on UID?

0 favourites
• 19 posts
From the Asset Store
Advanced inventory mechanics for your RPG game (Array-based). Take Items, split them, pick up them, read the description
• Hi Everyone,

Here is the scenario...

I have a mining ship and 2 asteroids.

When I right click the ship it uses the bullet behavior to move towards the asteroid I clicked.

When the ship gets within 100 pixels of the asteroid it stops using the distance(x,y,x,y) code.

All good so far and working as intended.

When I then right click the second asteroid, the ship moves towards it but doesn't stop at 100 pixels away. It bump right into it.

So it only seems to use the 100 pixels away part on the first asteroid clicked and not the second.

So I added a variable to the ship that saves the UID of the asteroid that was clicked.

So 2 issues...

1. Why does the distance check only work on the first asteroid clicked and not the second?

2. How do I use the distance(x,y,x,y) to test the distance to a specific UID and not just an object?

This gives an error:

Here is my code...

Thank you in advance.

• I think you could save the UID of the asteroid in a global var when you click it.

Then in the event where you put "distance(...", as an extra condition set Asteroid -> Pick by unqiue id and use the global var.

Does that help?

Cheers

• ## Try Construct 3

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

Construct 3 users don't see these ads
• Unfortunately a global car won’t work. Right now I’m just playing/learning/testing, the eventual goal will be to have many miners each with their own asteroids. So I can’t do a global vat in that instance.

So do I test the distance from the miner and the generic asteroid then as a second test assign asteroid to the saved UID?

• I think you missed a comma

` distance (X1, Y1, X2, y2)`

However, you can also use a function to solve this problem.

You can save the positions in two local variables and use them as the basis for calculating the distance

• You need to Pick by UID with your cached value before doing the distance check.

• You need to Pick by UID with your cached value before doing the distance check.

Can you elaborate on this please? I do not see a 'Pick by UID' option.

• Sprite:Pick by unique ID

• No change in the results.

• Repost your events.

• I don't know why it didn't attached the image...

• You need to pick the Asteroid, not the Miner.

Asteroid->Pick by unique ID

• The My_Asteroid has the UID of the asteroid needed. If I do just pick asteroid how will it know which asteroid?

• You've already got the code, but you are using Miner - change it to Asteroid.

(Keep the line number in your screen shot - it's easier to reference then).

• Ok, yes that worked.

Can you explain to me why it worked vs the selecting the miner?

• You stored the UID of the Asteriod. You can only use the value to pick that Asteriod again. Miner doesn't have that value, its UID will be different.

• 19 posts