playMaker

Author Topic: Is an Object in Front or Behind Another Object?  (Read 1957 times)

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Is an Object in Front or Behind Another Object?
« on: August 20, 2019, 02:18:46 PM »
Hello,

I'm trying to detect if an enemy is in front or behind the player but I'm not sure how. Right now I'm getting the position for both the player and the enemy and subtracting them with a Vector3 operator action to store the direction to the player. From there, I feel like I should be able to test if the direction to the player is positive or negative but I'm not sure how to do so.

Any help would be appreciated :)

Athin

  • Full Member
  • ***
  • Posts: 163
Re: Is an Object in Front or Behind Another Object?
« Reply #1 on: August 20, 2019, 09:31:04 PM »
Heya,

So what exactly are you trying to do with this as that will determine how to approach it.

What you can try is putting a collision box behind the player and parent to the player.  Anything within that box can be consider behind them but may not work if you need to detect further away.

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: Is an Object in Front or Behind Another Object?
« Reply #2 on: August 21, 2019, 12:14:08 AM »
Or raycast towards the player, with a hit box behind and a hit box in front, to see which one the ray hits?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is an Object in Front or Behind Another Object?
« Reply #3 on: August 21, 2019, 02:28:08 AM »
hi,

 Just check the distance to the camera for both your player and ennemy, use the square distance, it's faster then the real distance to to math.

 Bye,

 Jean

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Is an Object in Front or Behind Another Object?
« Reply #4 on: August 21, 2019, 10:08:21 AM »
Heya,

So what exactly are you trying to do with this as that will determine how to approach it.

What you can try is putting a collision box behind the player and parent to the player.  Anything within that box can be consider behind them but may not work if you need to detect further away.

I have the enemy jumping on the player if they get too close, so depending on what way the player is facing when this happens, it will play the corresponding animation set.

Or raycast towards the player, with a hit box behind and a hit box in front, to see which one the ray hits?

That was my first approach but I'm trying to avoid using unique hit boxes for everything so I'm not dealing with all kinds of layers. I just have a hard time warping my head around them and the further along the game gets, the more I'm likely to need. The less the better I'm thinking.

[...]
 Just check the distance to the camera for both your player and ennemy, use the square distance, it's faster then the real distance to to math.
[...]

Is this suggestion for a static camera? I have a free camera the player controls with the mouse or right analog stick on the game pad. It rotates around a 3rd person character so I'm not sure checking distance to it will work consistently. I could be misunderstanding you though. If you could explain in a little more detail.

--

I stumbled on to a kind of solution while looking at some C# scripts made for doing something similar:



Once the player is in range of the enemy, it gets the position of both the player and itself. It subtracts the two using the operator action to find the direction and then normalizes it. It takes the z direction of the player and turns it into a world space vector which is used in another operator along with the player transform to make a dot product which ends up being a float value somewhere around -1 or 1 depending on which way the player is facing.

This seems to work for the time being but I was using a lot of guess work to make it so I'm unsure if this is a good solution or if there is a nicer way to the same thing.

Thanks!