1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Stop Enemy from following

Discussion in 'Programming' started by euvue, Jan 7, 2019.

  1. euvue

    euvue Member

    Joined:
    Jan 7, 2019
    Posts:
    5
    Hello, First time here. I hope I've put this in the right place on the forum.
    I have a problem with my enemy. I want the enemy to follow the player if he's close enough but if the player run away I want the enemy to stop chasing him.
    The problem with my code is he wont stop. The chasing works great but he's just never stop chasing.
    I am a noob and tried to setup a enum for this.
    Here is my code:

    (enemy) Create Event:
    xspeed = 0;
    yspeed = 0;
    max_speed = 1.5;

    enum enemy {
    idle,
    chase
    }

    state = enemy.idle;
    sight = 100;

    (enemy) Step event:
    switch (state)
    {
    #region Idle State
    case enemy.idle:
    image_index = sEnemy1;
    if(instance_exists(oPlayer))
    {
    dis = point_distance(x,y,oPlayer.x,oPlayer.y);
    if(dis < sight)
    {
    state = enemy.chase;
    }
    }
    break;
    #endregion

    #region Chase State
    case enemy.chase:
    if(instance_exists(oPlayer))
    {
    var dir = point_direction(x,y,oPlayer.x,oPlayer.y);
    xspeed = lengthdir_x(max_speed, dir);
    yspeed = lengthdir_y(max_speed, dir);

    move_towards_point(oPlayer.x, oPlayer.y, max_speed);

    if (dis > sight) state = enemy.idle;
    }
    break;
    #endregion

    }
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,220
    The problem is that you are not reevaluating the variable "dis".
    The last time it gets updated is when the enemy changes to the chase state, but after that, "dis" will forever be less than "sight".
     
  3. euvue

    euvue Member

    Joined:
    Jan 7, 2019
    Posts:
    5
    Thank you! Now it's working :)
     

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