How do I tell if hit on rear or front of object?

0 favourites
  • 5 posts
From the Asset Store
50 front view of buildings JPG - residences shops and offices - 2048x2048
  • How would I go about determining a condition to either if an object is behind or in front of another object, in a top down shooter?

    Initially I was comparing the x and y locations, but the issue is that now both 'player' and 'enemy' objects can fully rotate and move about. So I'm experimenting with trying to compare angles relative to the 'target's' angle, but so far it doesn't work at all so I figure I must be taking the wrong approach.

    I found this from another thread but I can't seem to get it to work from a front/back standpoint:

    [quote:38z0tl9l]angle(player.x,player.y,object.x,object.y) is between player.angle and player.angle+179 (hit from right)

    angle(player.x,player.y,object.x,object.y) is between player.angle and player.angle-179 (hit from left)

    The game doesn't use physical objects, as it's a RPG pseudo-y game and 'attacks' are handled in functions which simulate dice rolls, damage etc etc, so I can't really use collision sprites, or at least I'm not sure how to without an actual projectile object.

  • Using the "is within angle" should do it.

    Angle(player.x, player.y, object.x, object.y is within 90 degrees of player.angle

    ---- (object is in front)

    Else

    --- (object is behind)

    That's for one player and one object. To work with multiple of each, add a "for each" player and object condition and put the events above as sub-events.

  • Many thanks, it seems to work well in my prototype, I didn't even notice that condition before!

    I'll try plugging it into my game tonight. My only concern is that I'm using the same object types for both the player and enemies. That's because they're all vehicles, and can be either AI or player controlled. The 'attack' function gets the 'firing vehicle' UID and 'target vehicle' UID passed to it as parameters, so I've been using that to pick between the instances.

    But if it's in the same condition, would I do something such as putting the objects into a family and using that to pick the second instance?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah, i'd use a family to handle two instances of the same type.

  • Works perfectly, thank you for the help!

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