I have a player, an opponent and a teammate.
I want to pass a ball from the player to the teammate and check that the opponent can’t intercept it.
If the player is facing the receiver - how do i find the position of the opponent relative to the player’s local space? How can I determine if the opponent is behind the player?
bool IsBehindMe(Transform me, Transform opponent)
{
return (Vector3.Dot(transform.forward, me.position -
opponent.position) > 0);
}
You can simply transform the opponent position into localspace by using InverseTransformPoint
of your player:
Vector3 oppLocalPos = transform.InverseTransformPoint(opponent.transform.position);
Then you can simply check where he is. If oppLocalPos.z is greater than 0 the opponent is in the front, if it’s smaller than 0 he’s in the back. If oppLocalPos.x is greater than 0 he’s on the right, otherwise on the left side. Same for “y” >0 above player <0 below player.