[SOLVED] How to measure distance between two entities in script

as in, calculate the distance between two objects and if the distance is less than a certain number do something.


Here is an example.

var distance = entityOne.getPosition().distance(entityTwo.getPosition());

if (distance < 10) {
    // do something
1 Like

alright i will try it

I keep getting an error stating “Can.getPosition is not a function”. It works when i use a name but does not work when I use a tag
Can you tell me what the problem is?

Probably because findByTag is returning an array of entities.

You can try the line below to use the first entity of the array.

var Can = this.app.root.findByTag('bottle')[0];

that does not seem to work I get this error
Cannot read properties of undefined (reading ‘getPosition’)
this problem exists even when there is only one entity with that tag

And I need it to return multiple entities, and use whichever number is the lowest.

Strange. What if you do it like below?

var Can = this.app.root.findByTag('bottle');
var Distance = Player.getPosition().distance(Can[0].getPosition());

This requires some extra logic that I don’t know at the moment.

Yes I figured it out