How to check the distance between two objects?

I’m trying to use the distance between two objects condition to check if the player is close enough to a container to be able to open it. I realized that this checks how far the player is from the other objects originpoint. I tried centering the originpoint of the container but that screws up the placement of the object on the grid. Is there a way to check the distance between the player and another point than the origin point? Or some other way to do this?

1 Like

Basic idea and probably not the best… But you can create a second “fake” object to test distance on it… Create this second object on container.PointX(Center);container.PointY(Center).

On the other hand, it’s strange… Take a look on this topic : [url]Some Performance Questions - #5 by Lizard-13] According to the sources, and Lizard has confirmed it, the distance condition uses the “Center” point.

That’s a good alternative, but to the second point, after further testing, I’m 100% convinced that it’s the origin point, unless my version 34 of GD5 is bugged.

Thanks for the advice.

Hi again. I tried to spawn a dummy, transparent object on the center point of the container but it spawns the object way off to the top left of it.

The blue box is 32x32 pixels and the point is spawned where the character man is standing. The box’s center point is set to auto and the origin is 0,0. I’ll upload the event I made for it as well.

EDIT: Here’s the event.

Hard to tell what’s going on like this… Try to reproduce what you want to do from a blank project (or reproduce the problem if you think it’s a bug, and send us the file).

Maybe you are using multiple layers/cameras and zoom levels ?

The point name is “Centre”, not “Center” :wink:
Note that objects are placed from their Origin point (the transparent object will be created so its Origin point is over the given position), so you have to take into account the Origin point of the transparent object too :slight_smile:

Haha. That’s a bit of a rookie mistake. Yes, since you helped me with the origin point of my bullet yesterday, I’ve kept that in mind. This is me most of the time = :astonished:

I tried to change it to centre but it’s still being created around the same location, way off the object…

You can send it to me in a PM or Discord :slight_smile:

Ok. I will! Thanks again Lizard!

Hi.
Recently I got same problem. I write for another people who want to solve this problem.
Just create two conditions distance between two objects, and invert one of them.
It means will be create border with two conditions.
And you can create many condition, for example, on 50, 100, 150 pixels between objects.
My case:
If the player close to the wolf less than 150, the wolf begin grow.
And if more than 150 - the wolf standby.
If less then 100 - the wolf attack the player.
But I dont know how to make more conditions. It will be may more fun and worked.

You need to use “Distance between objects” as condition, and choose player and wolf. If distance is less than or equal to 150, make the wolf grow.
Then add another similar condition with distance less than or equal to 100, and make wolf attack.
This could be optimized with nested conditions, but for now just focus on getting that to work with that more straightforward approach.

1 Like

Ok, but how can I begin to count of seconds before wolf attack?
The module on the image is not work.

In the first event, while the wolf is within 150 pixels of the player, the global variable Start_timer gets reset. Every single game frame.

Which means event 2 is actioned, and the timer “Wolf_attack_waiting_time” is reset. Every single frame that the wolf is within 150 pixels of the player.

Which in turn means event 3 is not actioned until the wolf is over 150 pixels from the player. And when it is actioned, it will for all wolves, not just the one that started the timer.


You’re much better off using an object timer and a variable to hold the attack state on each wolf. Something like this :

1 Like

Sorry to be late to reply; I’m only new to this forum, but I think I’ve found the answer. I had a similar problem when trying to get an object to activate when the player is near a point that isn’t the origin

I found a simple math equation that can test the distance between the two points.
In my case I compared the distance between the position of the player (Barry) and the point on my object (which was called “Box”, with points set to “origin” 0, 0 “centre” 32, 32 and “TheOtherPoint” 32, 64)

The equation compares the location of “Barry” and “Box > TheOtherPoint” and triggers the event if both the X and Y distance is less than 30 pixels.

If you use the compare two numbers event, and put the X location of 1 point minus the x location of another point, (make sure to use an absolute value so that it detects a negative distance) then set the comparison to less than the maximum distance (eg 30 pixels), it should detect when the player is within 30 pixels of the point
Then duplicate this event, replacing the X values with Y values.

Now when i press space, if the player is within 30 pixels X and Y from my point, it will trigger the action. I used a random action for the example, but you can replace it with the container opening sequence.
Also keep in mind that the trigger radius is now square, instead of circular, but that wont have much of an effect on game-play

Also in regards to your screenshot

i believe the issue was in using quotations “”

where your code said PointX(Center), i think it needed to be PointX(“Centre”).
hope that solves the issue