• 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 Instance exists in the line between 2 points

A

Antera

Guest
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