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

Get help using Construct 2

### » Sun Oct 11, 2015 11:11 pm

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:

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.
B
11
S
2
Posts: 13
Reputation: 601

### » Sun Oct 11, 2015 11:23 pm

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.
B
101
S
39
G
134
Posts: 5,592
Reputation: 85,522

### » Mon Oct 12, 2015 12:25 am

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?
B
11
S
2
Posts: 13
Reputation: 601

### » Mon Oct 12, 2015 5:57 am

Yeah, i'd use a family to handle two instances of the same type.
B
101
S
39
G
134
Posts: 5,592
Reputation: 85,522

### » Mon Oct 12, 2015 8:55 am

Works perfectly, thank you for the help!
B
11
S
2
Posts: 13
Reputation: 601