xcodegame-makergml

i can't use (point_distance) to kill any enemy nearset my hero GMK


I have a problem with a code in the Game Maker program I make a little game that has a path that the enemies walk on and put heroes on his side and when the enemy approaches him he turns to him and he shoots him I am using this code

var ex, ey;
ex = instance_nearest(x, y, enemy).x;
ey = instance_nearest(x, y, enemy).y;

if point_distance(x, y, ex, ey) < 150
{
     image_angle = point_direction(x, y, enemy.x, enemy.y);
}

The code works well, but the problem is that my heroes only go to the first enemy when they come close to them and do not go to the rest even when the first enemy is out of range What is the solution, please Photo for illustration Heroes ignore the nearby enemy and only head for the first enemy to appear in the game


Solution

  • You are checking the correct enemy but not pointing at the correct enemy. Store the found instance ID instead, and use it for all:

    var e;
    e = instance_nearest(x, y, enemy);
    if (e != noone) { // wouldn't want to crash when you run out of enemies
        if (point_distance(x, y, e.x, e.y) < 150) {
            image_angle = point_direction(x, y, e.x, e.y);
        }
    }