GML How to check the direction in which an object is moving?

P

PilsZuiger

Guest
Hi I am trying to create an if statement which checks in which direction a object is moving.

How can I accomplish this?

Maybe good to mention I am using Game Maker 8.1
 

kamiyasi

Member
It really just depends on how you're handling movement.
If you're using speed, hspeed, and vspeed, call "direction".
If you're updating x and y positions, you can use point_direction between xprevious, yprevious, x, and y.
 
P

PilsZuiger

Guest
So if I am using this :


I can use this code:
Code:
if(direction==left){
direction = right;
}
if(direction == right){
direction = left;
}
So if it moves to the right it starts moving to the left and visa versa
 

samspade

Member
Hi I am trying to create an if statement which checks in which direction a object is moving.

How can I accomplish this?

Maybe good to mention I am using Game Maker 8.1
Depending upon what you're using to move you can also do point_direction(x, y, hsp, vsp). Using xprevious and yprevious will return your past direction and using your current horizontal and vertical speed will return your future direction. Either may work or one may work better than the other depending on what you want.
 

samspade

Member
So if I am using this :


I can use this code:
Code:
if(direction==left){
direction = right;
}
if(direction == right){
direction = left;
}
So if it moves to the right it starts moving to the left and visa versa
I missed this post somehow. I believe that the drag and drop feature there does use the built in movement. However, direction is degrees (e.g. 0-359 practically if not technically) so you can't say direction == left or direction == right. If you want the direction in the 360 angle just use direction. However, if you want to know things like left or right, you could use a range where left is > 90 && < 270 and is everything else:

Code:
if (direction > 90) && (direction < 270) {
    dir = left;
} else {
    dir = right;
}
Note that 0 is equal to right not up. Alternatively, you can probably just say:

Code:
if (hspeed != 0) {
    dir = sign(hspeed);
}
 
P

PilsZuiger

Guest
I missed this post somehow. I believe that the drag and drop feature there does use the built in movement. However, direction is degrees (e.g. 0-359 practically if not technically) so you can't say direction == left or direction == right. If you want the direction in the 360 angle just use direction. However, if you want to know things like left or right, you could use a range where left is > 90 && < 270 and is everything else:

Code:
if (direction > 90) && (direction < 270) {
    dir = left;
} else {
    dir = right;
}
Note that 0 is equal to right not up. Alternatively, you can probably just say:

Code:
if (hspeed != 0) {
    dir = sign(hspeed);
}
okay thanks bro
 
P

PilsZuiger

Guest
I missed this post somehow. I believe that the drag and drop feature there does use the built in movement. However, direction is degrees (e.g. 0-359 practically if not technically) so you can't say direction == left or direction == right. If you want the direction in the 360 angle just use direction. However, if you want to know things like left or right, you could use a range where left is > 90 && < 270 and is everything else:

Code:
if (direction > 90) && (direction < 270) {
    dir = left;
} else {
    dir = right;
}
Note that 0 is equal to right not up. Alternatively, you can probably just say:

Code:
if (hspeed != 0) {
    dir = sign(hspeed);
}
I now tried using the piece of code you sent above but it says right is an unknown variable :S

Code:
___________________________________________
ERROR in
action number 1
of Collision Event with object obj_grass
for object obj_goomba:

Error in code at line 4:
       dir = right;
             ^
at position 12: Unknown variable right
 

samspade

Member
I now tried using the piece of code you sent above but it says right is an unknown variable :S

Code:
___________________________________________
ERROR in
action number 1
of Collision Event with object obj_grass
for object obj_goomba:

Error in code at line 4:
       dir = right;
             ^
at position 12: Unknown variable right
Dir is a custom variable, so you would have to create it somewhere. I would strongly recommend watching through a tutorial series on basic platformer movement. Or perhaps downloading some of the free marketplace resources for character movement.
 

jo-thijs

Member
Hi I am trying to create an if statement which checks in which direction a object is moving.

How can I accomplish this?

Maybe good to mention I am using Game Maker 8.1
As already mentioned, you check if you're going right by checking if the variable "direction" equals the number 0.
You check if you're going upwards by comparing "direction" with the number 90.
You check if you're going to the right by comparing "direction" with the number 180.
You check if you're going downwards by comparing "direction" with the number 270.
A direction in GameMaker is a counterclockwise angle in degrees with the positive x-axis.

So if I am using this :


I can use this code:
Code:
if(direction==left){
direction = right;
}
if(direction == right){
direction = left;
}
So if it moves to the right it starts moving to the left and visa versa
You can tackle that more efficiently.
Instead of checking your direction and switching it accordingly, mirror your horizontal movement.
You do so by setting the variable "hspeed" to "-hspeed".

that doesnt work :S
That's because it was a template code and it was designed to show you a principle,
rather than being a solution to your exact issue.


PS: You're Dutch too?
 
Top