# Finding which game objects lie at which sides or angles

I intend to determine the side at which there is no neighboring GameObject (which is a hexagon tile in my case). I am using OverlapSphere function to get the array of Collider objects.

``````Collider[] colliderObjs = Physics.OverlapSphere(go.transform.position, 0.7f);
``````

I can even access the position of adjacent gameobjects lying adjacent to the game object of my interest.

``````foreach (Collider cGO in colliderObjs){
Debug.Log(" Collider: "+cGO.transform.position);}
``````

However when I intend to find the angles at which the surrounding objects lie, I don’t get the correct results. I am using the following logic.

``````Vector3 dir = (obj.transform.position-current.transform.position).normalized;
float angle = Vector3.Angle(dir, trans.forward);
``````

The first line of the above code determines the direction at which the adjacent (or collider) object is located. Obj is the adjacent object while our object of interest is current.

The second line of code gives the angle at which it is located.

This logic isn’t giving me correct results. Any remedies?

Vector3.Angle returns an absolute value, thus you can’t say to which side the angle is. If you just want to know the side, you can use this:

```float angle1 = Vector3.Angle(dir, transf.forward);
if (angle1 < 90){
} else {
// object is behind
}
float angle2 = Vector3.Angle(dir, transf.right);
if (angle2 < 90){
// object is to the right
} else {
// object is to the left
}
```

Or you can combine the two cases and get the exact angle:

```float angle = Vector3.Angle(dir, transf.forward);
if (Vector3.Angle(dir, transf.right) < 90) angle = -angle;
```

This returns a positive angle to the left, and negative to the right (use > 90 if you want the opposite result).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

``````public class FindDirections : MonoBehaviour
{
[SerializeField] GameObject Target;

[SerializeField] Vector2 Fwd_MinMax = new Vector2(50f, 140f);
[SerializeField] Vector2 Side_MinMax = new Vector2(50f, 140f);

enum MyEnum
{
Bow,
Left,
Right,
Stern,
UnAssigned
}
MyEnum targetDirection = MyEnum.UnAssigned;

private void Update()
{
WhereIsTarget();
}

void WhereIsTarget()
{
Collider myCollider = transform.GetComponent<Collider>();
Vector2 myColliderCenter = new Vector2(myCollider.bounds.center.x, myCollider.bounds.center.z);

Vector2 target_asV2 = new Vector2(Target.transform.position.x, Target.transform.position.z);
float angle = Vector2.Angle(Vector2.up, target_asV2 - myColliderCenter);

if(angle <= Fwd_MinMax.x)
{
Debug.Log("Target in front");
targetDirection = MyEnum.Bow;
}

if(angle > Fwd_MinMax.x && angle < Fwd_MinMax.y)
{
float angle_XAxis = Vector2.Angle(Vector2.right, target_asV2 - myColliderCenter);

if(angle_XAxis <= Side_MinMax.x)
{
Debug.Log("Target right");
targetDirection = MyEnum.Right;
}
if(angle_XAxis >= Side_MinMax.y)
{
Debug.Log("Target left");
targetDirection = MyEnum.Left;
}
}

if(angle >= Fwd_MinMax.y)
{
Debug.Log("Target behind");
targetDirection = MyEnum.Stern;
}
}

private void OnDrawGizmos()
{
if(Target != null)
{
Vector2 tempColliderCenter = new Vector2(transform.GetComponent<Collider>().bounds.center.x, transform.GetComponent<Collider>().bounds.center.z);
Vector3 tempPos = new Vector3(tempColliderCenter.x, 0, tempColliderCenter.y);

Gizmos.DrawSphere(tempPos, .3f);
Gizmos.DrawLine(tempPos, Target.transform.position);
}
}
}
``````