• 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 Enemy Code

B

Bengs

Guest
Hello everybody i need a lot of help with my enemy code.

1) my enemy is a little buggy when hitting the floor
2) when my enemy is in patrol it wont even hit te floor it wil just float in 1 position
3) my enemy will always hit my character can i somehow randomize that process
4) When in patrol is ther a way to see what checkpoint it has hit so i can put walking animations in the right direction

Code:
player_x = Obj_Character.x;
player_y = Obj_Character.y;

//moving 
if (point_distance(x,y,player_x,player_y) < 100) {
    path_end();
    mp_potential_step_object(player_x, player_y, walksp, par_envirement)
    if player_x < x {
        image_speed =2/1.5;
        sprite_index = Spr_BasicKnightWalkLeft;
    } else if player_x > x {
        image_speed =2/1.5;
        sprite_index = Spr_BasicKnightWalkRight;
    }
   
} else if(path_index !=path_patrol) {
    mp_potential_step_object(start_x, start_y, walksp, par_envirement)
    if start_x < x {
        image_speed =2/1.5;
        sprite_index = Spr_BasicKnightWalkLeft;
    } else if start_x > x {
        image_speed =2/1.5;
        sprite_index = Spr_BasicKnightWalkRight;
    }
    if start_y < y {
    vsp = -3;
    vsp = vsp + grv
    y = y + vsp
    image_speed = 0;
    image_index = 0;
    }
        if (abs(x-start_x) < 2 and abs(y)) {           
            path_start(path_patrol, 2, path_action_restart, false);
        }
}

//Vertical Movement
vsp = vsp + grv
y = y + vsp
if (place_meeting(x,y+vsp,par_envirement))
{
    while (!place_meeting(x,y+sign(vsp),par_envirement))
    {
        y=y+sign(vsp);
    }
    vsp = 0;
}


hsp = facing_way * walksp;

//attack system
if (place_meeting(x+1,y,Obj_Character)){
    hsp = 1;
    x = x + hsp;
    image_speed = 2;
    sprite_index = Spr_BasicKnightAttack01Right;
    Edmg = 25;
    if (image_index > image_number - 1){
            attack = 0;
            Edmg=0;
            }
}
           
if (place_meeting(x-1,y,Obj_Character)){
    hsp = 1;
    x = x - hsp;
    image_speed = 2;
    sprite_index = Spr_BasicKnightAttack01Left;
    Edmg = 25;
    if (image_index > image_number - 1){
            attack = 0;
            Edmg=0;
            }
}
 

Simon Gust

Member
Your order of code is a little messed up.
You should try to follow this rule.
1. Inputs (in case of your object being an enemy, put AI logic here like following the player, attacking etc.)
2. motion (here you change hsp, vsp apply gravity to vsp etc.
3. collision (here you have your standard collision code with x and y updating after the collision code.
4. animation (You want to use only the most updated of variables to make your animation perfectly in sync with your logic)
 
Top