How can I create a 2D angle from a 3D angle, based on an objects orientation?

Ok so, I need to get a 2D angle between two GameObjects in 3D space. the 2D angle needs to be based on one of the objects up value.
Here’s what I have so far:
_
// gets diretion

Vector3 directionFromPlayer = ((ballLookAtPoint.position - playerLookAtPoint.position));

// set the angle

float angle = Vector3.Angle(directionFromPlayer, playerLookAtPoint.forward);

// (Because the angle has a min max of 0 to 180, and I need it to be -180 to 180)

Vector3 cross = Vector3.Cross(directionFromPlayer.normalized, playerLookAtPoint.forward);

if (cross.y > 0)

{

// make the value negative

angle = -angle;

}

_

First of all there is nothing like a 2d or 3d angle. An angle is a 1d scalar value. What you mean is a “signed” angle based on some reference. Any angle has small and a large angle since you can always do 360°-a to get the other one. Even in 2d it’s not clear / obvious which angle is meant (the small or the large one). In 2d one could easily use the winding order as a hint In 3d the hint has to be more precise since the two vectors can lie in any 2d plane.

If you want the angle in the horizontal plane, just make sure you eliminate the y components of your vectors before you calculate the angle. If you want the angle between your given vector and the forward vector of an object, you can use the dot product between your given vector and the “right” vector of your object to determine the sign.

Vector3 directionFromPlayer = (ballLookAtPoint.position - playerLookAtPoint.position);

directionFromPlayer.y = 0f;

float angle = Vector3.Angle(directionFromPlayer, playerLookAtPoint.forward);

if (Vector3.Dot(directionFromPlayer, playerLookAtPoint.right) < 0)
{
    angle = -angle;
}