GMS 2 How do I detect where an object is moving? (SOLVED)

K

komodolies

Guest
I have an "enemy" object with a point direction set torwards the player and I want to change the sprites attached to the object depending on if the object is moving up, left, down, et cetera. How would I detect this with the general direction it's moving isn't set on anything?
 

YoSniper

Member
If move_towards_point is the method you're going with, it might be easiest just to query what the object's direction attribute is, and set the sprite based on whether it's within a certain set of values (such as 45 degrees to 135 degrees would be UP.)
 
K

komodolies

Guest
Ah, yeah this makes sense. I've done something like this before with this code:
var dir = point_direction(x, y, mouse_x, mouse_y);

if (0 < dir and dir < 45 ) {
sprite_index = sprPlayerRight;
image_index = 0;
}

if (45 < dir and dir < 135 ) {
sprite_index = sprPlayerUp;
image_index = 0;
}

if (135 < dir and dir < 225 ) {
sprite_index = sprPlayerLeft;
image_index = 0;
}

if (225 < dir and dir < 315 ) {
sprite_index = sprPlayerDown;
image_index = 0;
}

if (315 < dir and dir < 0 ) {
sprite_index = sprPlayerRight;
image_index = 0;
}
}

So, basically just rehash this code but change the "dir" variable with "direction"? Sorry, I'm kinda new to programming lol

UPDATE: Yeah, this works exactly like I wanted it to. Thanks!
 
Last edited by a moderator:
Top