Detect in what sector is the Player around the NPC

Hi,
I’m trying to locate if a certain GameObject is interacting with another GO from the bottom, from the left, from the right, or the top.

So what I need is a function that returns from what type of area is the player trying to approach the NPC (Red, Blue, Green, Yellow)

I’ve trying with the following apporch:

void OnTriggerEnter2D(Collider2D collision) {
    Vector2 dist = this.transform.position - collision.transform.position;

    // Cos
    float angX = Mathf.Sqrt((Mathf.Pow(dist.x, 2) + Mathf.Pow(dist.y, 2)));
    angX = dist.x / angX;

    // Sin
    float angY = Mathf.Sqrt((Mathf.Pow(dist.x, 2) + Mathf.Pow(dist.y, 2)));
    angY = dist.y / angY;
}

but I don’t know how to proceed or if I’m doing wrong.

you should do

Angle = Mathf.Atan2(this.transform.position.y - collision.transform.position.y, this.transform.position.x - collision.transform.position.x) * Mathf.Rad2Deg;
1 Like

Thanks! You really saved my project!