• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [solved] Wrap direction to move towards the player?

hdarren

Member
I am making a top down shooter like Hotline Miami and there is a flying drone enemy that flies around. I want it to move towards the player but I want the direction it moves in to change slowly instead of abruptly.

What I have tried is to use point_direction() to get the direction of the drone to the player. I then compare it to the drone's current direction and increase/decrease the direction towards the intended direction.

I am having a specific problem though when it moves to the right due to how point_direction() works. For example if the current direction of the drone is 10 and the point_direction() is 350 then the drone's direction will increase to try to reach 350, but what it should do instead is decrease and wrap around from 0 to 360.

How do I perform this wrapping around correctly?

Thank you.
 

Alexx

Member
I use this script in my projects:
scr_angle_rotate:
Code:
/// @function scr_angle_rotate(angle1,angle2,speed)
//slowly rotates from current to target angle
return argument0 + clamp(angle_difference(argument1, argument0), -argument2, argument2)
And would call it from drone with something like (assumes only one obj_player in room):
Code:
///track target
if instance_exist(obj_player)
{
    target=obj_player;
}
else
{
    target=noone;
}
if target!=noone
{
    tx = target.x;
    ty = target.y;
    direction = scr_angle_rotate(direction, point_direction(x, y, tx, ty),3);
    image_angle = direction;
}
If you want a working example, let me know.
 
Last edited:
Top