How do I object attack one of the overlapping objects?

0 favourites
  • 6 posts
From the Asset Store
112 High-Quality destruction sounds for videogames
  • I'm making a project where you have your castle and need to defend him by sending troops that will fight the enemy troops and attack the enemy castle.

    When the troops begins to fight each other, they hurt all enemies that are being overlapped by them. A soldier dies with 4 hits of another soldier, this way, a single soldier can kill 3 overlapping enemy soldiers with 4 hits, a single arrow hurts 10 overlapping enemy soldiers, etc.

    I would like to fix this and make a soldier fights and hurts only one of the overlapping enemies in its front, make an arrow hits only one of the overlapping targets.

    How can I do it?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can do this in a few ways... I'm guessing that your enemy units are all individual sprites, and not one sprite that represents a whole group of enemies?

    If they're all separate, you can set the target to the projectile when the projectile is being created (on shoot). You could make some instance variables for the arrow, or the soldier, and call them something like TargetX and TargetY. Then you can pick the enemy that's closest to your defending unit, and set the instance variables TargetX and TargetY to be the coordinates of the enemy.

    You can then use the coordinates (pick by comparison) to tell your soldier or arrow which unit to apply damage (and also to trigger to destroy your arrow sprite). Simply compare the TargetX and TargetY values to the coordinate position of the enemy. I would recommend setting a comparison RANGE of say, 3-5 pixels or more (depending on the size of your graphics).

    Hopefully this hint helps. If you still have problems just let me know here and I will see if I can make a quick example.

    ~Sol

  • something like this should work:

    for each troop

    troop is overlapping enemy

    add subevent > enemy > pick nearest/furthest >

    x: troop.x

    y: troop.y

  • You can do this in a few ways... I'm guessing that your enemy units are all individual sprites, and not one sprite that represents a whole group of enemies?

    If they're all separate, you can set the target to the projectile when the projectile is being created (on shoot). You could make some instance variables for the arrow, or the soldier, and call them something like TargetX and TargetY. Then you can pick the enemy that's closest to your defending unit, and set the instance variables TargetX and TargetY to be the coordinates of the enemy.

    You can then use the coordinates (pick by comparison) to tell your soldier or arrow which unit to apply damage (and also to trigger to destroy your arrow sprite). Simply compare the TargetX and TargetY values to the coordinate position of the enemy. I would recommend setting a comparison RANGE of say, 3-5 pixels or more (depending on the size of your graphics).

    Hopefully this hint helps. If you still have problems just let me know here and I will see if I can make a quick example.

    ~Sol

    Hi, thanks for the atention.

    The sprites aren't individual sprites, they are all clones made by the system to defend the "CPU's" castle.

    My event sheet of the soldier being attacked by another soldier is:

    EnemySoldier is overlapping Soldier

    Is "Attack" animation playing.

    Animation frame="4"> Soldier subtract 25*EnemySoldier.PickedCount from life.

    The event for the arrow is:

    Soldier On collision with Arrow > Subtract 15 from vida.

    And then i'd like to maintain this codes and add the necessary to make sprites choose just one enemy at time to fight, and the arrows hurt just one enemy even when they are overlapped.

    A great exemple of what I'm talking is the game EpicWar 1 and 2. Even when the troops are all overlapped, they aim at only one sprite at time.

  • You mention that your enemies are "clones" of each other, which is fine. Is each "clone" though, just a SINGLE enemy as the sprite or do you have like 2 or 3 enemies per sprite that act as one object?

    If each enemy is it's own individual "clone" then you simply need to use object picking nearest/furthest as mentioned by celstrider. He basically explained in a shorter way what I was talking about in my post before. Just pick the enemy that's closest to your defender, and apply the damage to that enemy only (because you picked it specifically based on it's distance to the defending player).

    I think the reason that it's damaging ALL units right now, if because it hasn't been told to pick ONE unit specifically.

    ----------------------------------

    If overlapping ENEMY

    -> deal damage to enemy

    This will cause ALL enemies to be damaged when only ONE enemy is being overlapped.

    ----------------------------------

    If overlapping enemy & distance to enemy is less than 10

    -> deal damage to enemy

    This will cause only enemies that are within 10 pixels of you to take damage.

    ----------------------------------

    If overlapping enemy

    -> pick enemy closest to player

    ---> deal damage to enemy

    This will cause only the enemy that's closest to the player/defender/arrow to take damage.

    ----------------------------------

    The key is to make sure the game knows WHICH enemy you want it to pick, otherwise it will simply pick them ALL whenever there is overlapping.

    Hopefully this helps a bit more

    ~Sol

  • You mention that your enemies are "clones" of each other, which is fine. Is each "clone" though, just a SINGLE enemy as the sprite or do you have like 2 or 3 enemies per sprite that act as one object?

    If each enemy is it's own individual "clone" then you simply need to use object picking nearest/furthest as mentioned by celstrider. He basically explained in a shorter way what I was talking about in my post before. Just pick the enemy that's closest to your defender, and apply the damage to that enemy only (because you picked it specifically based on it's distance to the defending player).

    I think the reason that it's damaging ALL units right now, if because it hasn't been told to pick ONE unit specifically.

    ----------------------------------

    If overlapping ENEMY

    -> deal damage to enemy

    This will cause ALL enemies to be damaged when only ONE enemy is being overlapped.

    ----------------------------------

    If overlapping enemy & distance to enemy is less than 10

    -> deal damage to enemy

    This will cause only enemies that are within 10 pixels of you to take damage.

    ----------------------------------

    If overlapping enemy

    -> pick enemy closest to player

    ---> deal damage to enemy

    This will cause only the enemy that's closest to the player/defender/arrow to take damage.

    ----------------------------------

    The key is to make sure the game knows WHICH enemy you want it to pick, otherwise it will simply pick them ALL whenever there is overlapping.

    Hopefully this helps a bit more

    ~Sol

    IT WORKED!

    Thank you so much! You're awesome!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)