0
\$\begingroup\$

I'm making a game where the enemy can turn on its own axis in 360 degrees (top view game).

The player-controlled character has a bow. With this bow he shoots and deals damage when he hits the enemy.

I would like that when the arrow shot from the bow hits the enemy's back he would take double the damage.

I tried to make a system using arrow and enemy angles, but that system doesn't cover all cases.

Code:

if((image_angle-other.image_angle>280 && image_angle-other.image_angle<360) || (image_angle-other.image_angle<80 && image_angle-other.image_angle>0)){
    other.life-=damage*2;
}else{
    other.life-=damage;
}

Image of one of the inconsistencies that happen:

enter image description here

If the game's view was from the side, you could even use something using the angle the way I did, but as it's from above and the shot can come out at any angle and the enemy can be at any angle I couldn't think of anything.

In fact I even thought. Create an object that sticks to the enemy's back and is not visible to the player. Each detector object would have an enemy as its owner. If the arrow collides with that object, it would make the owning enemy suffer double damage.

However in the game there are arrows that go through enemies, so the arrows would end up hitting the detector object as well. I would have to make a way for the arrow to leave a mark on the enemy so that it doesn't take normal damage and then double damage (by colliding with the enemy and then with the detector object respectively).

Not to mention that from my knowledge I already knew that this alternative by itself is no longer ideal because it is not optimized, even if there was no double collision problem.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

FrostyCat's answer (member of the GameMaker community):


First consider the important angles involved (given that this is the arrow's collision event with a player):

  • The back-facing direction for the player: other.image_angle+180
  • Direction of hit from the player's perspective: point_direction(other.x, other.y, x, y)

Then use angle_difference instead of simple subtraction:

if (abs(angle_difference(point_direction(other.x, other.y, x, y), other.image_angle+180)) <= 40) {
    other.life -= damage*2;
} else {
    other.life -= damage;
}

Link:

https://forum.yoyogames.com/index.php?threads/how-to-detect-when-a-shot-hits-the-opponent-from-behind.87965/#post-526778

\$\endgroup\$
1
  • \$\begingroup\$ As I didn't get an answer that. I asked the same question elsewhere. I recommend doing this, it usually works for me, even the opposite (ask the question there, and then here, if you don't get an answer there). \$\endgroup\$ Commented Aug 4, 2021 at 23:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .