• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ Enemies follow the player without overlapping

Bochinator

Member
Hi, this is my first post. I've had GameMaker for a year now so I know the basics.

My current project is a simple arena shooter. The player object moves around the room and enemies follow it around. Originally, I used the mp_linear_step_object function for enemy movement. It worked pretty well in that enemies didn't overlap, but the enemies always moved in a direct line to the player's position, which looked really bad.

I fixed the movement issue by having all enemies rotate to the player's position. By setting direction to image_angle and giving them a speed, I got a really smooth-looking movement system. Increasing the rotation speed makes enemies more agile, while lowering it makes them take large arcs while following the player. The only problem with this is, over time, enemies objects with the same speed and rotation start to overlap, and eventually look like a single object.

I tried looking for the source code of the mp_linear_step_object to figure out how it works, but no luck. I'm hoping someone here can help. Thanks.
GML:
///This is in the step event of every enemy object

//Move forward

speed = spd;
direction = image_angle;
    
//Rotate to face the player

desiredAngle = point_direction(x,y,oPlayer.x,oPlayer.y);

distance1 = abs(image_angle - desiredAngle);
distance2 = 360 - distance1;
if (distance1 < distance2)
{
    smallDistance = distance1;
}
else
{
    smallDistance = distance2;   
}

targetValue = image_angle + smallDistance;
if (targetValue > 360)
{
    targetValue = targetValue - 360;   
}

if (targetValue == desiredAngle)
{
    image_angle += rSpd;
    if (image_angle > 360)
    {
        image_angle = image_angle - 360;   
    }
}
else
{
    image_angle -= rSpd;
    if (image_angle < 0)
    {
        image_angle = 360 + image_angle;   
    }
}

if (desiredAngle - rSpd <= image_angle) and (desiredAngle + rSpd >= image_angle)
{
    image_angle = desiredAngle;   
}
 
Top