- Home /

# How to get Yaw? or How to get Angle between two vectors over 180?

I wrote the code below to get Yaw. However, this code only gets -180 ~ 180 Yaw value. Let's try to illustrate what I want. I want the value to be 720 if the object rotate twice in the same direction.

```
public float GetYaw()
{
var down = -transform.up;
down.y = 0;
Yaw = Vector3.Angle(-Vector3.right, down) * Mathf.Sign(transform.up.z);
return Yaw;
}
```

**Answer** by JVene
·
Oct 01, 2018 at 02:01 AM

Let's be clear about one point in your question. There is no information that comes from vectors which can tell you how many times a rotation has been performed. If an object rotated 765 degrees clockwise in the xz plane, the only information you'll get back from the resulting information in a Transform will be that the object rotated 45 degrees clockwise, because that is all the information that remains.

Vector3.Angle returns the angle between two vectors, without regard to any axis. If you define yaw as a rotation in the xz plane, there is no relevance to down or up. I would infer that a relationship to the world 'negative right' direction and the transform's 'down' direction is more that of a pitch than a yaw, but Vector3.Angle is going to return the angle between 'negative right' and wherever 'down' is pointing, which won't be relative to an axis. Imagine pointing your left hand straight out left (that's what negative right means, no?), then point your right hand ahead of you, but down from straight out 45 degrees. Now, in yaw (the xz plane), your arms are 90 degrees apart, but the angle between your hands will be somewhere less than 90 but greater than 45 degrees, in a plane at an angle not aligned with a reference to a yaw.

You can get the rotation of an object (in the range of -180 to 180 degrees) using Mathf.Atan2, providing the coordinates you get from transform.forward ( x and z, ignoring y ). The return value is in radians which you can convert by multiplying the return by Mathf.Rad2Deg. Note, however, that the standard orientation may require adjustment to your expectations, because in math zero degrees (or zero radians) is on the X axis (to the right), and positive rotation is counterclockwise. Once you adjust that for your expectation, you'll get the angle in the xz plane the object is facing.

**Answer** by hannacho700
·
Oct 01, 2018 at 02:47 AM

I omited some explanation. In my case, transform.up points his backside. So down means forward. then I used "down.y =0" to bring vectors to xz plane. So I can already get yaw based on left(-Vector3.right) in xz plane. but It's range is -180~180. so I want to get more big size of range.

I think I solve my problem. So I share my idea.

```
public float GetYaw()
{
var down = -transform.up;
down.y = 0;
Yaw = Vector3.Angle(-Vector3.right, down) * Mathf.Sign(transform.up.z);
if (Yaw - preYaw < -300) round++;
else if (Yaw - preYaw > 300) round--;
roundYaw = round * 360 + Yaw;
preYaw = Yaw;
return roundYaw;
}
```

What do you mean by "get more big size of range". -180 to 180 is a full circle. There is no more to a rotation except spinning around and arriving at an angle inside that range. It could be interpreted as 0 to 360, which is basically the same thing.

If the object goes from -180 to 180, or from 180 to -180, the round variable is incremented or decremented, assuming that object turns around. And when you calculate the value, you can use round * 360 + yaw to get a value in the range greater than -180 to 180 , no matter how many turns the object is.

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

Set the rotation reference to an direction 0 Answers

Tangent Smoothing Angle on Procedural Mesh 0 Answers

Rotation strange 1 Answer