# Checking distance to player only in y direction

Discussion in 'Programming' started by mr_starfire, Feb 10, 2019.

1. ### mr_starfireMember

Joined:
Aug 5, 2016
Posts:
51
Hi guys .I have a pretty simple problem I have a trap object that should fall on the player as he/she passes by
I want to achieve this by checking below the object in the y direction to see if the player is there . i tried doing this using

if distance_to_object(o_player)

How would i check only in the y direction ?

I found this thread but it makes no sense to me https://forum.yoyogames.com/index.p...ance-between-two-objects-on-the-y-axis.16112/

I do not have a clue what to do with that piece of code I am aware that this is a problem with my intelligence not the answer .. Lol

2. ### TheouAegisMember

Joined:
Jul 3, 2016
Posts:
6,627
distance_to_object() is a 2-dimensional trigonometric calculation like finding the hypotenuse of a right triangle. It checks distance along x and distance along y, then finds the square root of the sums of the squares. You don't need to check x AND y, so there's no point using that function.

All that post says is you need to check the difference between y-coordinates. I don't get what's so nonsensical about it.

But if the trap is supposed to fall when the player is a certain distance away from it along the y-axis, that would mean if the player was close enough vertically to the trap, it would fall regardless of how far away the player is horizontally. The player could be halfway across the room and the trap would still fall.

Odds are you want to check something like

if player.y-y<64 && abs(x-player.x) < 16

3. ### mr_starfireMember

Joined:
Aug 5, 2016
Posts:
51
No as i said the post is fine its my brain that is broken..

if o_player.y-y<64 && abs(x-o_player.x) <16
{
triggered=1
}

I feel so dumb asking these questions hence my lack of posts . I rarely ask and would rather figure it out myself ..

The above does not work could you possibly break it down

4. ### RefresherTowelMember

Joined:
Jul 13, 2016
Posts:
1,176
I think it might be simpler. If you have a trap on the roof that is meant to fall when the player walks near it, simply do:
Code:
```if (abs(x-o_player.x) < 100) {
triggered = true;
}```
This will make the trap trigger when the player is within 100 pixels to the left or right of the trap. Try it out and see if it's what you want.

5. ### mr_starfireMember

Joined:
Aug 5, 2016
Posts:
51
Thanks that kinda worked .But throws up an error when the player dies dont worry guys we will be here all day lol I will figure it out somehow . Iam not as dumb as post makes me look i swear ..Thanks anyway

6. ### RefresherTowelMember

Joined:
Jul 13, 2016
Posts:
1,176
It throws an error because we're referencing the o_player by it's OBJECT name, rather than it's instance ID. We have to do that because we don't know how your game is setup. You should definitely look up the difference between objects and instances to get a better grasp of this. In the meantime, a quick fix is wrapping it inside this code:
Code:
```if (instance_exists(o_player)) {
if (abs(x-o_player.x) < 100) {
triggered = true;
}
}
```
You will also run into trouble if you have more than one instance of o_player in the game. If you were referencing an enemy (let's say it is called o_enemy) by using o_enemy.health and you have multiple o_enemies in the game, it would ONLY effect the first enemy created and no other enemies. This is why it is important to learn the difference between objects and instances.