1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GML Bullets not always hitting enemy

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

  1. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    Whenever the obj_leftBullet_shooter hits the enemy obj_water, the enemy is supposed to die in one hit. It works fine until I code in instance_destroy(); for the obj_leftBullet_shooter and I don't know why. If I code it so the bullet disappears once it hits the enemy, sometimes it'll take multiple hits before the enemy gets destroyed. This only happens once I've coded in the instance_destroy(); for the bullet.

    Current Code:

    obj_water, [event] collision with obj_leftBullet_shooter

    Code:
    ///Collision Effect
    
    //Die when hit by bullet
    
    with(obj_water){
        obj_water.hp -= 100;
    }
    
    instance_destroy();
    
    obj_water [event] step event

    Code:
    //Death
    if(hp == 0){
        instance_destroy();   
    }
    
    obj_leftBullet_shooter, [event] collision with obj_water

    Code:
    ///Destroy
    
    instance_destroy();
    


     
  2. Ratsha

    Ratsha Member

    Joined:
    Jun 22, 2016
    Posts:
    59
    In the collision event you can refer the keyword other, to the instance you're colliding with. You are currently taking ALL instances of obj_water and subtracting 100 hp. Instead you can simply write:

    Code:
    ///Collision Effect
    
    //Die when hit by bullet
    
    other.hp -= 100;
    instance_destroy();
     
    Amon likes this.
  3. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    I just get this error whenever I use other

    ___________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 1
    of Step Eventobj_leftBullet_shooter
    for object obj_water:

    Variable <unknown_object>.<unknown variable>(100002, -2147483648) not set before reading it.
    at gml_Object_obj_water_Collision_319995a3_8e03_44d1_9255_c897bbe70569 (line 5) - other.hp -= 100;
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Object_obj_water_Collision_319995a3_8e03_44d1_9255_c897bbe70569 (line 5)
     
  4. Ratsha

    Ratsha Member

    Joined:
    Jun 22, 2016
    Posts:
    59
    Oh, I assumed it was your Collision Event. Please let us review the collision detection in the step event as well, that's where it seems to go wrong.
     
  5. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    I was following a tutorial online and there wasn't any code for the collision in the step event only this:

    Code:
    ///Die & Follow Player
    
    //Death
    if(hp == 0){
        instance_destroy();   
    }
    
    //Call Script
    scr_enemy_follow()
    
     
  6. Ratsha

    Ratsha Member

    Joined:
    Jun 22, 2016
    Posts:
    59
    Put this code in the Collision Event with obj_water instead of step event:
    Code:
    ///Collision Effect
    
    //Die when hit by bullet
    
    other.hp -= 100;
    instance_destroy();
     

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