Finding the Angle Between Two Objects in 360 Degrees.

Trying to return values based on angle of object relevant to player but I can’t get it to do 360 degrees it only does 180. I searched for how to make it 360 degrees online but
still haven’t got it working. I know I need to differentiate between the left side and the right side which is why I added a right bool and the line
if (Vector3.Angle(target.right, target.position - target.position) > 90f) right = false; else right = true;
but it always returns right as true, any help would be appreciated.

using UnityEngine;
using System.Collections;

public class EnemyAngle : MonoBehaviour {

    public Transform target;
    public GameObject player;
    public bool right;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        target = player.transform;
    }
    void Update()
    {
        Vector3 targetDir = target.position - transform.position;
        Vector3 forward = transform.forward;
        float angle = Vector3.Angle(targetDir, forward);
        if (Vector3.Angle(target.right, target.position - target.position) > 90f) right = false; else right = true;
        if (angle < 45.0f && angle > 0)
        {
            Debug.Log("45");
        }
        if (angle < 90.0f && angle > 45.0f)
        {
            Debug.Log("90");
        }
        if (angle < 135.0f && angle > 90.0f)
        {
            Debug.Log("135");
        }
        if (angle < 180.0f && angle > 135.0f)
        {
            Debug.Log("180");
        }
        if (angle < 45.0f && angle > 0 && right == true)
        {
            Debug.Log("225");
        }
        if (angle < 90.0f && angle > 45.0f && right == true)
        {
            Debug.Log("270");
        }
        if (angle < 135.0f && angle > 90.0f && right == true)
        {
            Debug.Log("315");
        }
        if (angle < 180.0f && angle > 135.0f && right == true)
        {
            Debug.Log("360");
        }
    }
}

I’m coming back to this years later because it’s one of the top google answers. Here’s an easy solution.

float angle = Vector3.SignedAngle(vectorOne, vectorTwo, Vector3.up); //Returns the angle between -180 and 180.
if(angle < 0) angle = 360 - angle * -1;

There you go. Angle will now be from 0-360. No need to over complicate things.

There’s a much simpler way to determine whether the angle is to right or to the left when your axes are clearly defined.

When you calculate a Dot Product, two vectors facing within the same 180 degrees of each other result in a positive value, two vectors facing opposite directions are negative and perpendicular vectors results in zero.

How is this relevant? Well, If the dot product of transform.right and targetDir vectors is greater than zero, it’s to the right. If it’s less than zero, it’s to the left. Finally, if it is zero, it’s neither left nor right, but is either exactly in front of or exactly behind instead.

Now, this has nothing to do with why your current setup isn’t working, regardless. Why isn’t yours working at the moment?

Well, it’s due to a simple error:

if (Vector3.Angle(target.right, target.position - target.position) > 90f)

target.position - target.position == Vector3.zero; You’ve been testing the angle between the target’s right and Vector3.zero. You likely should have been comparing between transform.right and targetDir instead.

Vector3 targetDir = targetPos - originPos;

////180 to -180 angle
float angle = Vector3.SignedAngle(targetDir.normalized, new Vector3(0f, 0f, 1f), Vector3.down);
//                                                        //direction                   /Look vector                   /Rotate vector
////0 to 360 angle
float fullAngle = Quaternion.Euler(0f, angle, 0f).eulerAngles;
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);
        }
    }
}