• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Damage an enemy by shooting it in its back

Artie_Kyle

Member
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!
 

YoSniper

Member
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();
 

Artie_Kyle

Member
@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.
 
D

Danei

Guest
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?
 

Slyddar

Member
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:

Artie_Kyle

Member
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).
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!!!
 
Top