GML Instance exists in the line between 2 points

Antera

Member
Hello,

So a projectile is created from the player and moves to the nearest enemy
When a bullet meets a wall, it is destroyed

I'd like to automatically target the nearest_enemy that the bullet can reach without being destroyed by a wall

Code:
nearest_enemy = instance_nearest(x,y,oEnemy);

// TODO : check if there is a wall between x y and the nearest_enemy
// else choose 2nd nearest_enemy etc.

direction = point_direction(x, y, nearest_enemy.x, nearest_enemy.y);
speed = 16;
image_angle = direction;

How can do this check ?

Something like line_is_free_of_walls()

Many thanks for your help
 

YoSniper

Member
Script target_nearest_enemy(x, y);
Code:
///target_nearest_enemy(x, y)

/*RETURNS the id of the nearest targetable enemy. If none exist, returns noone.*/

var target, dist;
target = noone;
dist = 9999;

with(obj_enemy) {
    if not collision_line(x, y, argument0, argument1, obj_wall, true, true) and point_distance(x, y, argument0, argument1) < dist {
        target = id;
        dist = point_distance(x, y, argument0, argument1);
    }
}

return target;
 
Top