• 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!

checking if an enemy is behind the player

In my top-down action game I want to have it checked if an enemy is behind the player. does collision_line() work here, or only to check for objects in front?
 

Nidoking

Member
collision_line tells you what's along a line. Any line. Front, back, sideways, offset, top, bottom, completely random - you just put the numbers between the parentheses.
 

Sabnock

Member
How are you setting the direction of the instance you want to check if there is an enemy behind?

Once you know the physical direction you are facing it should be fairly simple to do the check.
 
I think
GML:
if collision_line(x, y, direction-180, direction-180, enemypar, false, false) //do stuff
works

But one more issue. It should check if the enemy is right behind the player, not miles away behind... if I use distance_to_object() it also counts the distance to enemy objects anywhere around the player; how to specifically count the distance to the object immediately behind the player object?
So that if the distance to the enemy right behind the player is f.e.x. smaller than 32, stuff happens?
 

Bentley

Member
collision line takes 4 coordinates. You gave it the x1 and y1 arguments, but not the x2 and y2 arguments. Something like this should do what you want:
GML:
// Is there an enemy behind me 32 pixels away?
var x2 = x + lengthdir_x(32, direction - 180);
var y2 = y + lengthdir_y(32, direction - 180);
var enemy = collision_line(x, y, x2, y2, enemypar, false, false);
if (instance_exists(enemy))
{
    // Stuff
}
So just to clarify, you're checking a collision line behind you, 32 pixels away, for any child of enemypar. If an instance exists, you have its ID saved in enemy. If there will be lots of enemies (think of zombies chasing you) you can use collision_line_list to get all of their IDs in a list and then you loop through that list.
 
Last edited:
collision line takes 4 coordinates. You gave it the x1 and y1 arguments, but not the x2 and y2 arguments. Something like this should do what you want:
GML:
// Is there an enemy behind me 32 pixels away?
var x2 = x + lengthdir_x(32, direction - 180);
var y2 = y + lengthdir_y(32, direction - 180);
var enemy = collision_line(x, y, x2, y2, enemypar, false, false);
if (instance_exists(enemy))
{
    // Stuff
}
So just to clarify, you're checking a collision line behind you, 32 pixels away, for any child of enemypar. If an instance exists, you have its ID saved in enemy. If there will be lots of enemies (think of zombies chasing you) you can use collision_line_list to get all of their IDs in a list and then you loop through that list.
big thanks. completely forgot about lengthdir . Ill try this one out when i get to go back to my project!
 
collision line takes 4 coordinates. You gave it the x1 and y1 arguments, but not the x2 and y2 arguments. Something like this should do what you want:
GML:
// Is there an enemy behind me 32 pixels away?
var x2 = x + lengthdir_x(32, direction - 180);
var y2 = y + lengthdir_y(32, direction - 180);
var enemy = collision_line(x, y, x2, y2, enemypar, false, false);
if (instance_exists(enemy))
{
    // Stuff
}
So just to clarify, you're checking a collision line behind you, 32 pixels away, for any child of enemypar. If an instance exists, you have its ID saved in enemy. If there will be lots of enemies (think of zombies chasing you) you can use collision_line_list to get all of their IDs in a list and then you loop through that list.
works fine indeed. big thanks @Bentley !! also @Nidoking @Sabnock !
 
Top