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

Game Mechanics (SOLVED) Enemies Following Player -- Is There a Better Way?

Discussion in 'Game Design, Development And Publishing' started by guitarmonkeys14, Jan 28, 2019.

  1. guitarmonkeys14

    guitarmonkeys14 Member

    Joined:
    Jan 19, 2019
    Posts:
    7
    Hey everyone,

    I am currently making a top down shooter in which I have successfully made the enemy always follow the character. I have hit the point where I feel comfortable enough figuring out my own ways of making everything happen.

    Initially I wanted to do something along the lines of an If obj_player exists, but I could find anything along those lines with Drag and Drop. So, I decided to link the enemies movement to whether the player had HP or not, I just wonder if this is the best and most efficient way.

    This causes a script error after my player is killed, but I think that is because the enemies have nothing to point towards. I don't think this will be an issue down the line because I will have a respawn/restart feature shortly.

    Code:
    // If Variable
    with(obj_player) var l0C47E57A_0 = HP > 0;
    if(l0C47E57A_0)
    {
       // Set Point Direction
       direction = point_direction(x, y, (obj_player.x), (obj_player.y));
    
       // Set Instance Rotation
       image_angle = point_direction(x, y, obj_player.x, obj_player.y);
    
       // Set Speed
       speed = 1;
    }
    
    upload_2019-1-27_18-49-38.png upload_2019-1-27_18-50-38.png

    Ideally I would like to find a way to make the enemy continue straight down mindlessly when the obj_player is destroyed.

    Suggestions please??
     
  2. guitarmonkeys14

    guitarmonkeys14 Member

    Joined:
    Jan 19, 2019
    Posts:
    7
    Well, after about two hours of trial and error I answered all of my questions..

    Why would I link to HP when a distance to object exists???

    And I figured out making the enemy continue down after player destruction


    For anyone out there who may be having trouble

    a) getting enemies to follow your player and
    b) deciding how they react if your character dies

    This is what I did

    Code:
    if(distance_to_object(obj_player) > 2000)
    {
       image_angle = 270;
    
       direction = 270;
    
       speed = 1;
    }
    
    if(distance_to_object(obj_player) <= 2000)     // set to 2000 (greater than max game size, plus some because enemies spawn outside of room) - I want my enemies always following the player, the only time they don't is if player is destroyed
    {
       image_angle = point_direction(x, y, obj_player.x, obj_player.y);     // To make the enemy face the player at all times
    
       direction = point_direction(x, y, (obj_player.x), (obj_player.y));     // So that when speed is applied it moves towards the player
    
       speed = 1;     // Change this anywhere from (.001 - 1000) to customize speed
    }
    
    Hope this helps my fellow n00bs ;P

    For you experienced guys out there, please feel free to correct/tweak/advise wherever possible ;)
     
    Last edited: Jan 28, 2019
  3. Miradur

    Miradur Member

    Joined:
    Jan 16, 2018
    Posts:
    110
    Hi, you know the command instance_exists(object)?

    Code:
    if instance_exists(obj_Player)
       {
       do something ....
       } else 
       Enemy goes down ....
       }
    There are also lots of commands to work with objects, so say goodbye to the idea of using their ID as a number.


    Miradur
     
    guitarmonkeys14 likes this.
  4. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    912
    Yes, do not ever type out the object ID as a way to reference an object. This is terrible coding practice. Use the functions that are designed to return the object number, rather than hard-coding them (also, make sure you are paying attention to the difference between an object ID and an instance ID...If you have more than one instance of an object in a room, NEVER use the object ID to reference it, always get the instance ID using any of the multiple functions that return an instance ID).
     
  5. guitarmonkeys14

    guitarmonkeys14 Member

    Joined:
    Jan 19, 2019
    Posts:
    7
    Exactly what I was looking for, thank you so much!
     

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