GameMaker Studio: Enemy Chase AI Tutorial



Chase behaviour for a platformer enemy / character. Part of a series of videos coming on different enemy behaviours for platformers etc.

Find these videos useful? Consider supporting on Patreon:

Download the project files:

More GameMaker tutorials at:

I believe in accessible education, this video’s content and the included GameMaker source files are and always will be free for anyone to use in their projects, commercial or otherwise. No credit or consent is needed to use this code in your own work, I merely ask that the video and/or source files themselves are not redistributed as they are without my consent.

Nguồn: https://infernalaffairsguild.com/

Xem thêm bài viết khác: https://infernalaffairsguild.com/game/


Article Categories:
Game

Comments

  • This doesent work on 8.1 🙁

    Zack_Yodaz July 8, 2020 6:48 am Reply
  • The enemy doesn't follow me, but instead seems to do every move I do

    TheBulbaMachine July 8, 2020 6:48 am Reply
  • Instead of going towards the player, when I go in the radius, my enemy walks away only a few pixels and follows me when I leave it only a few pixels. I don't know what to do

    David Adepeju July 8, 2020 6:48 am Reply
  • the download link dosent work anymore

    JustZeikin July 8, 2020 6:48 am Reply
  • it just destroys itself when i come near it, i am using gms2 but is that different? if so how do i fix it?

    Jelly Joel July 8, 2020 6:48 am Reply
  • Is there any fix here if the enemy is walking in slopes because they are spazzing whenever walking downwards.

    Kahon Games July 8, 2020 6:48 am Reply
  • I added something like that into the game, but when my Player dies the game crashes. Because the chase enemy is still looking for the Player object but it no longer exists.

    ThatAtrikus July 8, 2020 6:48 am Reply
  • I run into a problem with this.
    Because of how I set up my respawn mechanic, my obj_player doesn't exist for about 62 frames when it dies.
    So my Game crashes whenever I die.

    I tried to make it so that this whole switch only activates:
    if object_exists(obj_player)
    {
    // all of that code
    }

    But that doesn't seem to solve it.
    amateur here… first time working with this and these videos are incredible, thanks so much for any help

    Muffin Heretic July 8, 2020 6:48 am Reply
  • enum doesnt work for some reason. it doesnt event turn yellow when i write it, its like my gamemaker doesnt have enum…

    meganoX93 July 8, 2020 6:48 am Reply
  • what if my enemy is floating or flying since this only works on ground troops

    Alexander Armstrong July 8, 2020 6:48 am Reply
  • At what point are you defining dir ? I'm not seeing it in the enemy's create event.

    jayholden July 8, 2020 6:48 am Reply
  • Hey guys i don't have an enum constant, what can i use for replacement?

    Skirm Mish July 8, 2020 6:48 am Reply
  • Hi Shaun, I wanted to know if you accept freelance requests ?

    Romain Courtois July 8, 2020 6:48 am Reply
  • my enemy doesnt follow me, my enemy sticks to the ground and sinks down to the bottom of my land and when it has reached the bottom of the land/ground it freezes my game

    billySMOKER144 July 8, 2020 6:48 am Reply
  • How do I make it so it avoids walls, but still makes it's way around them?

    Calibur July 8, 2020 6:48 am Reply
  • What if you want the enemy to face the direction that the player is, that doesn't apply here because your enemy is a cube, my enemy has a face..and it looks weird for him to be facing left and following the player that is in the right side..

    Nizar Lahmar July 8, 2020 6:48 am Reply
  • Thanks for the video, Shaun. For some reason, my enemy will follow my character, but it will only stop moving once it reaches my character's right side. For instance, if my character is to the left of the enemy, the enemy will chase my character, and stop once it collides with him. However, if I run PAST the enemy to its right side, the enemy will push through my character and stop moving once it is positioned to the right of my character. Any suggestions?

    Giant Mushroom Tree July 8, 2020 6:48 am Reply
  • Very useful tutorial! I use this for my spider enemy of my pixel art survival platformer Droseria :))

    EngineerZ.Games July 8, 2020 6:48 am Reply
  • How can you stop the spazzing when above or under player

    Eglaf July 8, 2020 6:48 am Reply
  • Hi my name is markiplier and i'm here to teach you how smash a bottle against your head. First empty bottle. Then smash!!!! smash sound effect ouch!!!!!

    Sillyguydefender July 8, 2020 6:48 am Reply
  • is it okay if the text is red? (e_state)

    euanPC July 8, 2020 6:48 am Reply
  • what is " sign" in GameMaker

    Steven Chen July 8, 2020 6:48 am Reply
  • The code doesn't show any errors, but if I run the game, and move within range of the enemy, It doesn't chase/attack me. Here's my code:

    switch(state)
    {
    case e_state.idle:
    {
    hsp = 0;
    vsp = (min(7, vsp+0.05));
    if (distance_to_object(Obj_player)<128) state = e_state.chase;
    }
    break;
    case e_state.chase:
    {
    dir = sign(Obj_player.x – x);
    hsp = dir*2;
    vsp = (min(7, vsp+0.05));
    if (distance_to_object(Obj_player)>128) state = e_state.idle;
    }
    break;
    }

    MattJohno2 July 8, 2020 6:48 am Reply
  • Hey game makers. I keep getting this compile error stating that " case argument should be constant." ( ps i literely copied and paste the code shaun made so nothing here is edited or any thing

    switch (state)
    {
    case e_state.idle: <– – this one is giving me problems
    {
    hsp = 0;
    vsp = (min(7,vsp+0.05));
    if (distance_to_object(obj_player) < 96) state = e_state.chase;
    }
    break;
    case e_state.chase: <— and this one
    {
    dir = sign(obj_player.x – x);
    hsp = dir * 2;
    vsp = (min(7,vsp+0.05));
    if (distance_to_object(obj_player) > 128) state = e_state.idle;
    }
    break;
    }

    Pablo.DonkeyBong.2009 July 8, 2020 6:48 am Reply
  • Any possibility of explaining how to implement pathfinding AI with this code so that monsters don't get stuck on walls?

    B White July 8, 2020 6:48 am Reply
  • How can i make it jump? I do all same like in the video.

    Jakub Dudek July 8, 2020 6:48 am Reply
  • why my gamemaker tell me there is something wrong with creation code
    enum e_state help plz ?

    God Goku Play July 8, 2020 6:48 am Reply
  • So I did something a little different from the tutorial, and I would like someone's input on why it still works. I made a script and did the enum code. Then, I put the rest of the code in a step event. But I didn't at any point call upon the script in that code. Is the simple fact it is enum making it work, even though the script itself wasn't called upon? Or is the enum code relevant at all for this to work, and he was doing it to simply show us the tip of a different concept?

    Robert Haney July 8, 2020 6:48 am Reply
  • Hi can anybody help me, my enemy is always in the chase state. here's my code:
    switch (state)
    {
    case e_state.idle:
    {
    hsp = 0;
    vsp = (min(7,vsp+0.05));
    if (distance_to_object(obj_player) < 96) state = e_state.chase;
    state = e_state.chase;
    }
    break;
    case e_state.chase:
    {
    dir = sign(obj_player.x – x);
    hsp = dir * 5;
    vsp = (min(7,vsp+0.05));
    if (distance_to_object(obj_player) > 128) state = e_state.idle;
    }

    }

    //Horizontal Collision
    if (place_meeting(x+hsp,y,obj_solid))
    {
    while(!place_meeting(x+sign(hsp),y,obj_solid))
    {
    x += sign(hsp);
    }
    hsp = 0;
    }
    x += hsp;

    //Vertical Collision
    if (place_meeting(x,y+vsp,obj_solid))
    {
    while(!place_meeting(x,y+sign(vsp),obj_solid))
    {
    y += sign(vsp);
    }
    vsp = 0;
    }
    y += vsp;

    CoolSmile July 8, 2020 6:48 am Reply
  • This guy's the best. If you gotta problem, he already has a tutorial on it.

    Nattington July 8, 2020 6:48 am Reply
  • is there a way to have the enemy idle until the player comes with in range and it just turns on and stays on with its movement like in mario removing the cache function

    TheShadow Pony July 8, 2020 6:48 am Reply
  • my game will never run no matter which tutorial i use.

    is it because I'm using the mac version?

    Chicken Bigmac July 8, 2020 6:48 am Reply
  • i added this symbol { at line 4 pos 6 and it's saying that it's an error idk what to do

    Hard Time Games July 8, 2020 6:48 am Reply
  • my enemy keeps clipping through the floor.. how to fix pls..

    jaminjar July 8, 2020 6:48 am Reply
  • Here is my enemy chase AI script: I get my whole game paused every time the enemy is too far from me instead of just him. Any help?
    switch (state)
    {
        case e_state.idle:
        {
            hsp = 0;
            vsp = (min(7,vsp+0.05));
            if (distance_to_object(obj_player0)<96) state = e_state.chase;
        }
        break;
        case e_state.chase:
        {
            dir = sign(obj_player0.x-x);
            hsp = dir * 2;
            vsp = (min(7,vsp+0.05));
            if (distance_to_object(obj_player0)>128) state = e_state.idle;
        }
        break;
    }
    //Horizontal collision
    if (place_meeting(round(x+hsp),round(y),obj_wall))
    {
        while (!place_meeting(round(x+sign(hsp)), round(y), obj_wall)) x+= sign(hsp);
        hsp = 0
    }
    x += hsp;//Vertical collision
    if (place_meeting(round(x),round(y+vsp),obj_wall))
    {
        while (!place_meeting(round(x), round(y+sign(vsp)), obj_wall)) x+= sign(hsp);
        vsp = 0
    }
    y += vsp;

    Sunder Mind July 8, 2020 6:48 am Reply
  • i've created walls in my game and my enemies look like they have wall hack, how can i limit their aggro distance so it doesn't spot me on the other side of the wall?

    Making4Fun AE July 8, 2020 6:48 am Reply
  • PLEASE HELP!
    It says there's an error with this, line 12: dir = sign(obj_player.x – After the game is played, it says error: Unable to find any instance for object index '0' name 'obj_player'
    at gml_Object_obj_enemy_StepNormalEvent_1 (line 12) – dir = sign(obj_player.x – x);

    I have an animation set up. Maybe thatws the problem. PLEASE HELP!

    Gravilocity July 8, 2020 6:48 am Reply
  • I was trying to pick this out of the video but I couldn't… can anyone tell me how to have an enemy chase the player all the time no matter how far away they are or anything like that? (so pretty much how to do this with only one constant state) thanks.

    James Smith July 8, 2020 6:48 am Reply
  • how can you make the enemies jump to try and catch you

    Gummi Squad July 8, 2020 6:48 am Reply
  • 0:23

    Megumin July 8, 2020 6:48 am Reply
  • Okay I did the code exactly the way he shows in the video but e_state, idle, and chase keep showing up as red and are registered as errors. Does anyone else have this problem?

    Aubrey Wilson July 8, 2020 6:48 am Reply
  • can you please help :

    _________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 1
    of Step Event0
    for object object21:

    Variable object21.e(100006, -2147483648) not set before reading it.
    at gml_Object_object21_StepNormalEvent_1 (line 14) – if (distance_to_object(Vantagees) > 150) state = e.state_idle;
    ############################################################################################

    this error keeps popping up and idk how to fix it
    Vantagees is just what i named my character
    the chase if statement is if(distance_to_object(Vantagees) < 150) state = e.state_chase;

    ArviKreon July 8, 2020 6:48 am Reply

Leave a Comment

Your email address will not be published. Required fields are marked *