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

add motion away from an enemy

W

wizwaz

Guest
Hello,

I'm quite new to GML and what I'd like to be able to do is if one of my enemies is say less than 70 pixels away from another enemy, then add some sort of motion to move away from that enemy so to try and avoid moving into it as both enemies are moving towards the player.

This is the code that moves the enemies:

Code:
motion_add(point_direction(x,y,object_player.x,object_player.y),0.1);
Any advice is much appreciated.
 

samspade

Member
Hello,

I'm quite new to GML and what I'd like to be able to do is if one of my enemies is say less than 70 pixels away from another enemy, then add some sort of motion to move away from that enemy so to try and avoid moving into it as both enemies are moving towards the player.

This is the code that moves the enemies:

Code:
motion_add(point_direction(x,y,object_player.x,object_player.y),0.1);
Any advice is much appreciated.
Something like this might work:

Code:
motion_add(point_direction(x,y,object_player.x,object_player.y),0.1);
with (obj_enemy) {
    var this_enemy = id;
    if (this_enemy == other.id) {
        continue;
    }
    var dist = point_distance(x, y, other.x, other.y);
    if (dist < some number) {
        with (other) {
            motion_add(point_direction(this_enemy.x, this_enemy.y, x, y),0.1);
        }
    }
}
Basically, inside of an enemy instance, you loop through all instances of obj_enemy. The first thing you do is check and make sure that it's not itself, then if it isn't, you see how far away it is. If it is close enough, with the original enemy (and while confusing you can call a with inside of a with and in this case with (other) refers to the original enemy instance) use the motion add again. Notice that in this case your point direction is from the other enemy to the current enemy as you want it to be away. Also, I'm using this_enemy rather than other inside of the with loop as I can't remember how GML handles the keyword other inside of a nested with.

An alternative version would be to loop through all enemies, save those enemies to an array, and then loop through the array and use motion add like so:

Code:
var close_enemy_array = [];
var i = 0;
with (obj_enemy) {
    if (id == other.id) {
        continue;
    }
    var dist = point_distance(x, y, other.x, other.y);
    if (dist < some number) {
        close_enemy_array[i] = id;
        i++;
        //you can combine the above two lines like this close_enemy_array[i++] = id;
        //as that uses the value of i to assign and then increments it
        //but I wouldn't unless that makes sense to you
    }
}

for (var i = 0; i < array_length_1d(close_enemy_array); i += 1) {
    var enemy = close_enemy_array[i];
    motion_add(point_direction(enemy.x, enemy.y, x, y), 0.1);    
}
 
W

wizwaz

Guest
Hi,
Many thanks samspade for taking the time to explain, I really appreciate it.
 
Top