- Home /

# 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;

}

This code works if im on the ground and a the same elevation

**Answer** by Bunny83
·
Feb 13 at 10:23 PM

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;
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Manual Quaternions 3 Answers

unity landscape issue 1 Answer

3D ball control issues 0 Answers

iOS Screen Resolution bug when start in landscape 1 Answer

Android Game Orientation cannot be fixed to potrait 0 Answers