1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 enemy move toward player (GMS2)

Discussion in 'Programming' started by KPJ, Feb 12, 2019.

  1. KPJ

    KPJ Member

    Joined:
    Jan 19, 2019
    Posts:
    115
    Hi everyone! I need help. I want the enemy to move toward the player if the player shoots within a certain range. At first, all my code was working and the enemy moved toward the player but then all of a sudden it stopped working and the enemy didn't move.

    Code:
    //if player shoots
    with (oEnemy)
    {
        if (distance_to_object(oPlayer) < bulletalertrange)
        {
            placeofinterestx = oPlayer.x;
            placeofinteresty = oPlayer.y;
            estate = estates.chasealert;
        }
    }
    
    //Enemy create
    grid = mp_grid_create(0, 0, room_width/32, room_height/32, 32, 32);
    mp_grid_add_instances(grid, oSolid, false);
    path = path_add();
    
    //Enemy step
    if (estate == estates.chasealert)
    {
        ChaseAlert();
    }
    
    //Enemy chase alert script
    nearestdoor = instance_nearest(x, y, oDoor);
    
    mp_grid_path(grid, path, x, y, placeofinterestx, placeofinteresty, true);
    path_start(path, enemyspd, path_action_stop, true);
    
    if (x == placeofinterestx) && (y == placeofinteresty)
    {
        if (alarm[2] == -1)
        {
            alarm[2] = 5;
        }
    }
    
    if (distance_to_object(nearestdoor) < 20)
    {
        with (nearestdoor)
        {
            if (closing)
            {
                opening = true;
                closing = false;
            }
        }
    }
    Anyone know what's happening? Thanks!
     
  2. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    537
    I would say its that line of code here, as if it gets to where the player was at the time of calling the code and the player moves, the enemy will just move to where the player was, not where it is at the given time

    I have a tutorial on mp_girds if your interested, you could plug your code into it and it should work
     
    KPJ likes this.
  3. KPJ

    KPJ Member

    Joined:
    Jan 19, 2019
    Posts:
    115
    Evan Malinowski that's how I want it to work. I want the enemy to investigate the players wherabouts at that moment in time. (sorry for not making that clear) The problem is however, the enemy does not move at all. One second it was working perfectly but the next it doesn't even move
     
  4. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    537
    is your enemyspeed starting at 0? or is that a set speed the enemy can go at any time?
     
    KPJ likes this.
  5. KPJ

    KPJ Member

    Joined:
    Jan 19, 2019
    Posts:
    115
    My enemy speed is set to 4.5 from the start (the create event of the enemy). I also noticed something else. If my enemy is behind a wall but within 100 px of the player, that's when it doesn't work. However, if the enemy is within the range and not behind a wall (clear sight of the player) it works. I don't know why though
     
  6. Lady Glitch

    Lady Glitch Member

    Joined:
    Feb 10, 2019
    Posts:
    20
    Is oDoor a child of oSolid?
     
    Bayesian, EvanSki and KPJ like this.
  7. KPJ

    KPJ Member

    Joined:
    Jan 19, 2019
    Posts:
    115
    Yes, it is a child of oSolid
     
  8. Lady Glitch

    Lady Glitch Member

    Joined:
    Feb 10, 2019
    Posts:
    20
    That's why it doesn't work. Your enemy sees it as a wall:
    Code:
    mp_grid_add_instances(grid, oSolid, false);
     
    Bayesian and KPJ like this.
  9. KPJ

    KPJ Member

    Joined:
    Jan 19, 2019
    Posts:
    115
    Lady Glitch Wow! Thanks so much! I completely forgot that I changed it from oWall to oSolid. It works perfectly now. Thanks for all your help Lady Glitch and Evan Malinowski :)
     
    Lady Glitch and EvanSki like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice