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

GML Damage an enemy by shooting it in its back

Discussion in 'Programming' started by Artie_Kyle, Feb 13, 2019 at 2:54 AM.

  1. Artie_Kyle

    Artie_Kyle Member

    Joined:
    Jul 30, 2018
    Posts:
    108
    What up GMC!

    The title says it all; I've been trying to damage and enemy only when I fire bullets at its back so it acts as the weak spot, but I can't quite get the code to run.

    Here's the code, let me know what I'm doing wrong:
    Code:
    // code belongs to the bullet object, in the collision event with the boss object
    
    var _left = place_meeting(x-1, y, obj_boss);
    var _right = place_meeting(x+1, y, obj_boss);
    
    if _right and obj_boss.sprite_index == spr_boss_standing and obj_boss.image_xscale == 1 {
           scr_damage(20, obj_blood_grey);
    }
    
    if _left and obj_boss.sprite_index == spr_boss_standing and obj_boss.image_xscale == -1 {
           scr_damage(20, obj_blood_grey);
    }
    
    instance_destroy();
    
    For the record, the damage script works like a charm with all the other enemies, so I know its not the culprit here, but probably the place_meeting code, although I still can't quite come up with the fix.

    As always, thank you all for your time!
     
  2. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    175
    What is the movement code for the bullet?
     
  3. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    298
    The thing about place_meeting is that it checks for the collision as if the object (and its entire mask) were shifted to the given position.

    I imagine that your obj_boss is a pretty big target, and maybe your bullet object flies at a high speed (i.e. much greater than 1.) This means that a bullet travelling from the right might still trigger the _left condition, and vice versa.

    To combat this, I recommend just using this for the obj_bullet Collision with obj_boss event.

    This also assumes that the object's sprite origins are centered. If they are not, then you will have to adjust the x parameter accordingly.

    Code:
    if other.sprite_index == spr_boss_standing { //Overarching condition. It's the same for either scenario
        if (x > other.x and other.image_xscale == 1)
        or (x < other.x and other.image_xscale == -1 {
            scr_damage(20, obj_blood_grey);
        }
    }
    
    instance_destroy();
     
    Bentley likes this.
  4. Artie_Kyle

    Artie_Kyle Member

    Joined:
    Jul 30, 2018
    Posts:
    108
    @YoSniper

    Tried the code, but it didn't work. Overarching condition bit was pretty good advice, so I'm grateful for that one!

    And yeah, everything's centered as well.

    @Danei

    Code:
    ///scr_enemy_fire();
    ///@arg target
    ///@arg projectile
    ///@arg weapon
    ///@arg projectile_parent
    
    
    var _target = argument[0];
    var _projectile = argument[1];
    var _weapon = argument[2];
    var _projectile_parent = argument[3];
    
    if instance_exists(_target) {
       var _ldx = lengthdir_x(5, _target.x);
       var _ldy = lengthdir_y(5, _target.y);
    
       var _bullet = instance_create_layer(x, y, "Instances", _projectile);
       
       with _bullet {
           depth = _weapon.depth-1;
           phy_bullet = true;
           physics_apply_impulse(x+_ldx, y+_ldy, (_target.x + random(10)-5 - x) * 200, -(y - _target.y+ random(10)-5) * 200+random(100));
           phy_rotation = -point_direction(x, y, _target.x, _target.y);
       }
    }
           
    _weapon.trigger_ = true;
       
    var _damage = _projectile_parent;  
    _damage.creator_ = id; 
       
    sniper_timer_ = room_speed;
    sniper_trigger_ = true;
    
    The bullet works with every other object it collides with, so I don't think the bullet code is the issue.
     
  5. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    175
    I guess I mostly just wanted to see how fast the bullet moves. But I don't understand the physics functions so the joke's on me. If it gets all the way inside the boss, both _left and _right conditions would be true, but I guess we already determined that's not the issue?

    What exactly is happening instead of what you want to happen?
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,046
    He needs to make sure the rotation of the bullet is 180° roughly of that of the enemy
     
  7. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    671
    How are you keeping track of the bullet and enemies drawing direction? Looks like you are using image_xscale, as oppsosed to a variable like "facing", which is then drawn. However you are doing it, as long as the facing or image_xscale values are equal, they are both moving in the same direction, so the boss would be hit in the back if there was a collision.
    Code:
    if obj_boss.sprite_index == spr_boss_standing and obj_boss.image_xscale == obj_bullet.image_xscale {
        //moving in the same dir, so if colliding, it's in the back
        scr_damage(20, obj_blood_grey);
    }
    instance_destroy();
    
    If you are just using direction and speed for the bullet, you can find the moving direction by sign(hspeed).
     
    Last edited: Feb 13, 2019 at 4:14 AM
    Artie_Kyle and Bentley like this.
  8. Artie_Kyle

    Artie_Kyle Member

    Joined:
    Jul 30, 2018
    Posts:
    108
    Sly, you sly devil, it worked!

    Well, technically I had to change one thing due to using physics, but your logic was absolutely flawless.

    Here's the final code:
    Code:
    
    var _bullet_direction = sign(phy_speed_x);
    
    if //obj_damnation.sprite_index == spr_damnation_standing
    obj_damnation.image_xscale == _bullet_direction {
           scr_damage(20, obj_blood_grey);
    }
    
    I don't know how I messed the sprite standing bit, because it turned out to be a hindrance when all was set and done. I debugged everything and saw that your logic should definitely work when the numbers were all equal at the appropriate times. Then I figured that this bit is useless code, because the code would work without it anyways (only time you can hit the enemy in the back is when its standing anyways).

    Cheers, Sly, and everyone who has given their input, truly appreciate it! ON TO THE NEXT HEAD SCRATCHER, ROCINANTE, O'NOBLE STEED!!!
     
    TheSly likes this.
  9. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    671
    Haha, all good, you're welcome. In my course I have a section on blocking an arrow with a shield, so I had used the same principal there. It's always the simple solutions... ;)
     
    Artie_Kyle likes 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