- Home /

# Rotation angle from one object to another

Hi,

I'm trying to ge the rotation angle from my transform to another object.

Here's a little picture to describe what I mean

What I'm trying to achieve is the following: Get the rotation angle from my transform to another gameobject. So that 0° is forward of my transform (The biggest cube in this case). C#

Please let me know if I haven't explained this good enough :)

Thanks, Andreas.

**Answer** by diegzumillo
·
Dec 07, 2013 at 09:40 PM

If I get what you're asking this is a simple task. You want the angle between the direction the first cube is looking and the position of the other cube? Then it's something like

```
Vector3.AngleBetween( object 1 forward, object 1 position - object 2 position );
```

Thank you very much :D I knew there were a simple way to achieve this.

I tried using this function, but it always returns a positive value, meaning I cant tell if the object is 35° left or right from me, which - in fact - sucks. So i have to calculate this manually... Thanks Unity.

Any angle between two vectors can be interpreted in two different ways. 35° is the "small" angle and 325° is the "large" angle. Both represent the same angle just the other way round. To define a specific orientation you need to specify a reference frame. Two 3d vectors could be oriented any way in space. Without another reference vector there is no concept of "left" or "right".

Well, thats your way of interpreting it... I'd say If I especially want to calculate the angle in respect to my forward vector and in 2D only, I'd need an indication if its positive or negative... but whatever. I've come up with this code now:

```
// the position to compare with
var targetPosition = new Vector3();
// use my gameobject's transform to determine fwd vector to target
var localTarget = transform.InverseTransformPoint(targetPosition);
// Use Trig to get my Angle IN RANGE -180 to 180
var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
```

"left or right of me" I imagine you are working in 2D then. In 3D left/right don't make much sense and it requires more information, maybe a vector itself. Anyway, maybe an easier way is to just use a cross/vector product. It will be a vector pointing in the z direction, and it will be positive or negative depending on whether the vector is 'to the left or right' so to speak.

**Answer** by Noxrawr
·
Dec 07, 2013 at 10:25 PM

Hi, this should work.

```
Vector3 direction = (target - transform.position).normalized;
float angle = Vector3.Angle (direction, unit.transform.forward);
```

**Answer** by UziMonkey
·
Apr 09, 2017 at 10:44 PM

A simple **Vector3.Angle** call will work here, but usually when I do this I want to know what the angle between them in relation to some plane. If you were to do this:

```
Vector3.Angle(transform.forward, other.position - transform.position);
```

You will get the angle between the two through a plane defined by, I think, the first point and the cross product between the two vectors. If the objects are always on the same plane, that's fine, it'll be the same thing. However if one object is elevated above the grid in your image then the angle will not be the same, and the further the object is away from the grid plane the more wrong the result of his function will be.

So instead you can do the same thing, but project the vectors onto a know plane, in this case a plane defined by **Vector3.up**.

```
var angle = Vector3.Angle(
Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized,
Vector3.ProjectOnPlane(other.position - transform.position, Vector3.up).normalized
);
```

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