# Proximity/Distance Check

#### Lord Vectra

##### Master Eventer
I am Vectra. I've gotten a request from a friend to do this. I did this for someone else a long time ago so i figured I'll make a tutorial for all who wants to know how.

Here's what you need:
4 Variables
4 Conditional Branches

What are the 4 variables?:
X-Coord1
Y-Coord1
X-Coord2
Y-Coord2

Let's say you want to detect the distance between the player and a rock:

To find the x & y-coord of the rock/person
Go to Control Variable
Go to Game Data <= only skip this ONE step if you use Vx
Go to Character
Pick the character (player or the event)
On the right pick MAP Y or MAP X

Red is where you pick which event you want to know the coordinates of and the blue is whether you want MAP X or MAP Y. There are other choices, but they are irrelevant. Don't use Screen X or Y because that uses coordinates on-screen and you don't want that.

PL is Player and RO is the rock.
X-Coord = X coordinate
Y-Coord = Y coordinate

You subtract the X-Coords to find the distance horizontally and you subtract the Y-Coords to find the distance vertically.

The multiplying by -1 is so that the number will become positive if the answer happens to be a negative. In mathematics, distance cannot be a negative number.

Due to the X-Coord and Y-Coord check needing to be 1, the system checks for diagonals too.

You minus the rock's coordinates with the player's coordinates. If the answer is 1 or 0 then they're touching. If you want it like have it where if the rock is 5 steps away then you simply make the condition branches 5 or less instead of 1 or less.

It'll look like this:
This checks if it's 5 steps away horizontally and then checks vertically.

Wait, but what if I want distance between two events?
Then you replace player with another event. For example, If i wanted the distance between 2 rocks then I simply replace X and Y Coord1 with another event's X and Y Coord instead. If you want to know the distance of more than 2 events then every event added needs 2 variables(X & Y Coord).

SUB-TUTORIAL: DISTANCE
Only difference is you scrap the Y-Coord PL check and add X-Coord and Y-Coord right before the proximity (which is now distance) check.

Note: Make sure both types are on parallel process unless you want it to check the distance once.

If you have ANY questions plz ask.

Last edited:

#### Andar

##### Veteran
One comment:
the "wait" command is neccessary to reduce lag in parallel processes because it basically tells the computer "skip this parallel process for x frames", which means it will only be executed every sixth frame (ten times per second) instead of every frame (sixty times per second).
And you don't need to check the positions more often that the player or event moves.
Depending on how fast that movement is, a developer might get away with a higher wait (reducing processing time further), but if the wait is too high the event might miss a move and doesn't work correctly. That is why the number of waitframes need to be set individually for the specific case where the position is checked.

However, because that wait command determines when the process is skipped, it is usually placed at the end of the event after the check run through the first time instead of at the beginning (delaying the first check)

#### Zeriab

##### Huggins!
The difference is pretty insignificant. The player is only likely to notice the difference if it's possible to be teleported from another map into the range of the check. Apart from that it's more a matter of stylistic taste. Being consistent in whichever you choose is more important than which style you choose.

Please note that the check here is if the player is within a 11x11 box centered around the target.

Another way of proximity checking is to look at the Manhattan distance. Mathematically the check looks like this:
Code:
``(x1 - x2).abs + (y1 - y2).abs <= dist``
Consider this to be a diamond based area check.

Yet another way is the Euclidean distance which is the distance had the player not been locked to a grid in terms of movement, but instead could approach it directly. Mathematically the check looks like this: (By x**2 I mean x * x)
Code:
``(x1 - x2)**2 + (y1 - y2)**2 <= r**2``
I recommend this approach if you are using pixel movement while I recommend the Manhattan distance for the normal tile-based movement. I am not a fan of the box proximity check, at least not as the normal go-to check.

*hugs*
- Zeriab

#### Lord Vectra

##### Master Eventer
I am well aware of the distance formula; I do it this way so the person can choose to check vertically or horizontally. That's why I changed the title to Proximity, but I guess I'll add it in as a sub-tutorial.

EDITED: It has been added at the bottom

Last edited:

#### HexMozart88

##### The Master of Random Garbage
Pretty blessed, pretty blessed. Nice job, though I shouldn't have to be telling you. XD

#### Serg

##### Veteran
Yes! That's what I was looking for! Thank you so much!

#### Zeriab

##### Huggins!
Thank you ^^

Your choice to use == 5 instead of <= 5 on the distance tutorial is quite interesting.
I didn't consider it before, but having the proximity not trigger when you are too close to something can be useful.

*hugs*

#### Dope

##### Veteran
Thanks for this guide! I was looking all over for a way to create proximity events and this does exactly what I need! I'm pretty sure I can think of other uses, but for now I'm combining it with the terrax lighting script so that I have dark dungeons that have lights that glow when the player walks within a certain proximity of said light. Seriously, thanks, I most likely would not have figured this out on my own.

#### Lord Vectra

##### Master Eventer
Thanks for this guide! I was looking all over for a way to create proximity events and this does exactly what I need! I'm pretty sure I can think of other uses, but for now I'm combining it with the terrax lighting script so that I have dark dungeons that have lights that glow when the player walks within a certain proximity of said light. Seriously, thanks, I most likely would not have figured this out on my own.

#### mobiusclimber

##### Veteran
Is there any way to set this up so it can check where an event is in relation to the player, i.e., above, below, left or right? I'd like to use this to trigger battles from on map enemies, and to solve puzzles.

#### Lord Vectra

##### Master Eventer
Do you see how in the 2nd picture, I have it if the X or Y result is less than 0, multiple it by -1? You would take that out.

Now, since you're taking the * -1 part out, the order you subtract is very important.

Event X(or Y) - Player X(or Y) = Tells you where the event is in relation to the player

Player X(or Y) - Event X(or Y) = Tells you where the player is in relation to the event

X = Right
-X = Left
Y = Up
-Y = Down

So you would check to see if X and/or Y is positive or negative. So if you wanted to give them 200 gold if the event is below them, then you would Event Y - Player Y (AKA Control Variables[Event Y] -= Variable: [Player Y]) then do a conditional branch of conditional branch with "If Event Y > 0, then 200 gold."

Does that make sense? I can further elaborate if needed.

#### mobiusclimber

##### Veteran
Makes sense to me. Thanks so much for your help and for the awesome tutorial!

### Latest Profile Posts

Me: "Finally finished most of my game backlog. Probably time to do some gamedev" | Steam: *offer sales*
If I recorded while I created (what I think is) a small plugin, would you be interested enough in the process to watch it?
I keep trying to explain to clients that the huddle masses behind the old K-Mart are a Fallout Reenactment group. Somehow I don't think they believe me.
Hey people! What do you think about a core plugin? This will save some lines of code that I repeatedly use in all my plugins...
Wondering if I should make my enemies scale in my non-linear rpg. I'm leaning towards yes, but I don't want to use Yanfly's plugin...