• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Legacy GM [Unsolved] Sword Collision Multiple Enemy Instances

H

healthyaddiction

Guest
So I used the Shaun Spalding tutorial on swinging a sword and checking collision with an enemy and created some code that looks like this:
Code:
    if ((image_index >= 1) && (image_index <=3)) {
        with (instance_create(x,y,obj_skitter_hitbox)) {
            image_xscale = other.image_xscale;
            with (instance_place(x,y,obj_Robot)) {
                if (hit == 0) {
                    hit = 1;
                    state = enemy_hurt_state;
                }
            }
        }
    }
This works perfectly for hitting one enemy instance, but I want the sword to hit multiple enemies if they're close enough and this doesn't do that.

I looked online and found someone who advised to use distance instead of collision, so I made this:
Code:
    if ((image_index >= 1) && (image_index <=3)) {
        with (instance_create(x,y,obj_skitter_hitbox)) {
            image_xscale = other.image_xscale;
            with(obj_Robot) {
                if (point_distance(x,y,other.x,other.y) < 70) {
                    if (hit == 0) {
                        hit = 1;
                        state = enemy_hurt_state;
                    }
                }
            }
        }
    }
This does allow me to hit multiple enemies but a) doesn't seem to detect the hit as consistently and b) I can hit enemies stood behind me (because they're still within range).

Could anyone advise me on what is the best solution to this? Ideally I'd like to use the top version as the collision detection seems to be more consistent than the distance detection. But if the second is needed to be used a tweak to fix my problems would be greatly appreciated.

Kind Regards
 
B

Boogery Boogers

Guest
So I used the Shaun Spalding tutorial on swinging a sword and checking collision with an enemy and created some code that looks like this:
Code:
    if ((image_index >= 1) && (image_index <=3)) {
        with (instance_create(x,y,obj_skitter_hitbox)) {
            image_xscale = other.image_xscale;
            with (instance_place(x,y,obj_Robot)) {
                if (hit == 0) {
                    hit = 1;
                    state = enemy_hurt_state;
                }
            }
        }
    }
This works perfectly for hitting one enemy instance, but I want the sword to hit multiple enemies if they're close enough and this doesn't do that.

I looked online and found someone who advised to use distance instead of collision, so I made this:
Code:
    if ((image_index >= 1) && (image_index <=3)) {
        with (instance_create(x,y,obj_skitter_hitbox)) {
            image_xscale = other.image_xscale;
            with(obj_Robot) {
                if (point_distance(x,y,other.x,other.y) < 70) {
                    if (hit == 0) {
                        hit = 1;
                        state = enemy_hurt_state;
                    }
                }
            }
        }
    }
This does allow me to hit multiple enemies but a) doesn't seem to detect the hit as consistently and b) I can hit enemies stood behind me (because they're still within range).

Could anyone advise me on what is the best solution to this? Ideally I'd like to use the top version as the collision detection seems to be more consistent than the distance detection. But if the second is needed to be used a tweak to fix my problems would be greatly appreciated.

Kind Regards
The way you currently have it, "with (instance_place(x,y,obj_Robot))" is only gonna take the oldest instance ID of "obj_Robot" and do whatever you want to do to that very first instance ID only.

One easy way to get it to recognise the attack on multiple enemies, is put the collision check with your hitbox on the "obj_Robot" instead.
 

Aviox

Member
A combination of the two would work:
Code:
if ((image_index >= 1) && (image_index <=3)) {
   with (instance_create(x,y,obj_skitter_hitbox)) {
      image_xscale = other.image_xscale;
      with(obj_Robot) {
         if (place_meeting(x,y,obj_skitter_hitbox) {
            if (hit == 0) {
            hit = 1;
            state = enemy_hurt_state;
            }
         }
      }
   }
}
As Boogery Boogers pointed out, instance_place(), as well as any collision checking event, will only return the first collision it finds, not multiple, so you have to branch out further with a "with" statement, or a collision event in obj_Robot or obj_skitter_hitbox.

If there's a chance for multiple "obj_skitter_hitbox" to exist at once, you'll want to save the ID of the one you just created, and only check for a meeting with that specific instance
 
H

healthyaddiction

Guest
Hey guys, thanks for the help. I went with Aviox's solution and it seems to be working brilliantly!

Greatly appreciated.
 
H

healthyaddiction

Guest
Thanks for your help earlier this week, but I've been having a small problem with the script above. Sometimes a hit is being registered on another enemy that isn't within collision distance when I hit a different enemy that is within collision distances (so both near and far are hit). It does only seem to happen in specific circumstances and it only seems to happen when the enemy is in chase mode, not when they're idle. However it seems very random. I think the enemy has to be moving but still quite far away from my character, but it's quite hard to pinpoint.

Could this be something to do with the above script or must it be something with the rest of my code?
 

Aviox

Member
The block of code I posted above doesn't calculate distance, so it doesn't matter how far away an enemy is from the player. It only checks if there's a collision with "obj_skitter_hitbox". Perhaps I'm misunderstanding what you mean by that.

Either way - is there anywhere else where you use the object "obj_skitter_hitbox"?

How are instances of this object destroyed?
 
H

healthyaddiction

Guest
Hey, yeah I know the distances isn't calculated, however enemies that aren't touching the collision box are being hit when I successfully hit an enemy within the collision box. This is only in certain circumstances such as those I spoke about above.
Nowhere else is using obj_skitter_hitbox, except for the jump attack but that shouldn't cause conflict and doesn't seem to affect this if removed.
Instances are destroyed in the enemy hurt state once their health reaches 0.
 
Top