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

SOLVED "AI" Dodge or avoid bullets

Viciousse

Member
Hi everyone,

I am trying to add an "AI" dodge of player bullet; with the following:

- Enemie can move right or left to avoid bullets
--- Move left if there is no obstacles (other enemies or other instances) otherwise move right if possible.
- Most important: Dont go toward bullets
- Bullet can bounce a few times

I am using the code bellow, but its not working as i was expecting:

GML:
move_wrap(true, false, sprite_width);

if (distance_to_object(obj_ball) < 150) {
    if (distance_to_object(obj_robot) < 50) {
    sprite_index = spr_robot_right;
    x +=  1;
    } else {
        sprite_index = spr_robot_left;
        x +=  -1;
    }
} else {
    sprite_index = spr_robot;
}
Is there any better way doing this other then "distance_to_object"?
 
Last edited:

Fabseven

Member
Maybe you could trigger the enemy behaviour when you shoot the bullet ?

If the bullet is going straight line maybe you could detect thats the bullet will hit the monster and do trigger the moving behaviour

I think there is a function collision line that could do the job (but i think it will only work for the first mob )
 

Nidoking

Member
You probably want to do something with point_direction and/or lengthdir_x to determine whether the thing it's dodging is to the left or right and dodge in an appropriate direction. Ideally, you'd trace forward the path of both instances to see where they're going to be and try to avoid collisions that way.
 

Viciousse

Member
Juste Edited,
(The bullet (obj_ball) can bounce a few times.)

Do you think lengthdir_x and/or lengthdir_y is usable in this situation?
 

Viciousse

Member
Okay, i am trying the code bellow:

Enemie Step Event:
GML:
move_wrap(true, false, 0);

if (!instance_place(x, y, obj_robot) && place_empty(x, y, obj_ball)) {
    if (distance_to_object(obj_ball) < 150) {
        if (distance_to_object(obj_robot) < 50) {
            sprite_index = spr_robot_right;
            x +=  3;
        } else {
            sprite_index = spr_robot_left;
            x +=  -3
            }
    } else {
        sprite_index = spr_robot;
        }
}
It seems to "work" so far, but, the "move_wrap" part make the enemis unkillable, any idea how i can add an offset to it?

and sometimes, the enemis stacks, what kind of function can i use to avoid that?

Thanks
 

Viciousse

Member
So, i found a way to make it work!

I'll drop this here maybe someone will need it some day :)

I created a left and a right rectangle and to ennemys "dodge" when the "obj_ball" entrer the rectangle:

Step Event:
GML:
// Doge and avoid ball
if ((collision_rectangle(x, y - 50, x+50, y+50, obj_ball, true, true)) ^^ (collision_rectangle(x, y - 50, x+50, y+50, obj_robot, true, true))) {
    sprite_index = spr_robot_left;
    x -= robot_spd; //left
    
   } else if ((collision_rectangle(x, y - 50, x-50, y+50, obj_ball, true, true)) ^^ (collision_rectangle(x, y - 50, x-50, y+50, obj_robot, true, true))) {
    sprite_index = spr_robot_right;
    x += robot_spd;
   } else {
       sprite_index = spr_robot;
   }
 
Top