# Finding the angle between a direction and a point

**URL:** https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345
**Category:** Scripting
**Tags:** Scripting
**Created:** [September 19, 2009, 8:54am UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345 "2009-09-19T08:54:22Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![blastone](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@blastone](https://discussions.unity.com/u/blastone)
#### Post date: [September 19, 2009, 8:54am UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/1 "2009-09-19T08:54:22Z")

</div>

I wish to find the angle (theta) between an objects transform.forward vector and an arbitrary target point.

Ive tried doing a Vector3.Angle on the transform.forward vector and subtracting that from the Vector3.Angle of the target postiion - the transform position but the numbers seem all skewed.

I’m using it in a 2d environment.

Im sure there is a really easy way… I’m just missing it…

 ![199314--7330--$angle_491.jpg](https://europe1.discourse-cdn.com/unity/original/4X/d/2/8/d28896ea49947baf9a59b89b869342f6f7b82908.jpeg)

---

<div class="post-metadata">

### Author: ![jcarpay](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jcarpay](https://discussions.unity.com/u/jcarpay)
#### Post date: [September 19, 2009, 1:45pm UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/2 "2009-09-19T13:45:18Z")

</div>

Here the math that should get you started

 ![199355--7334--$calcangle_141.gif](https://europe1.discourse-cdn.com/unity/original/3X/5/0/50fa8b1c2c3c5e81a22635a47e3f6326947674ca.gif)

---

<div class="post-metadata">

### Author: ![fallingbrickwork](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fallingbrickwork/32/400505_2.png) [@fallingbrickwork](https://discussions.unity.com/u/fallingbrickwork)
#### Post date: [September 19, 2009, 3:13pm UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/3 "2009-09-19T15:13:49Z")

</div>

> [@""](#):
>
> Here the math that should get you started

wouldn’t this only be correct if the target point is, in this case on the zero x axis, making the triangle a right angle? I need to do something similar and my trig maths was a long time ago.

Matt.

---

<div class="post-metadata">

### Author: ![jcarpay](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jcarpay](https://discussions.unity.com/u/jcarpay)
#### Post date: [September 19, 2009, 4:22pm UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/4 "2009-09-19T16:22:05Z")

</div>

> [@""](#):
>
> wouldn’t this only be correct if the target point is, in this case on the zero x axis, making the triangle a right angle? I need to do something similar and my trig maths was a long time ago.
> 
> Matt.

If you rotate the right triangle (making it non zero x), it still would be a right triangle. The extra calculations involved are to find the perpendicular distance between the top of the triangle and its foot. For this, dot product is your friend.

---

<div class="post-metadata">

### Author: ![blastone](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@blastone](https://discussions.unity.com/u/blastone)
#### Post date: [September 19, 2009, 6:18pm UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/5 "2009-09-19T18:18:37Z")

</div>

thanks for the replies, I am down with the trig solution, I was just wondering if there was a smart in built function that I missed.

I’ll google the dot product as well or would projecting the transform onto the target vector suffice…

Thanks again

---

<div class="post-metadata">

### Author: ![rozgo](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/rozgo/32/136952_2.png) [@rozgo](https://discussions.unity.com/u/rozgo)
#### Post date: [September 20, 2009, 3:46am UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/6 "2009-09-20T03:46:44Z")

</div>

Vector3.Angle should do what you want and Vector3.Cross should give you the axis to rotate around of.

---

<div class="post-metadata">

### Author: ![blastone](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@blastone](https://discussions.unity.com/u/blastone)
#### Post date: [September 20, 2009, 4:09am UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/7 "2009-09-20T04:09:37Z")

</div>

```auto
Vector3 targetDir =m_waypoint.transform.position - transform.position;
		targetDir = targetDir.normalized;
		
		float dot = Vector3.Dot(targetDir, transform.forward);
		float angle = Mathf.Acos( dot ) * Mathf.Rad2Deg;

```

This is what seems to work well for me,  
the angle is always positive, so I also use a cross product

```auto
Vector3 normalizedDirection = m_waypoint.transform.position-transform.position;
		
		float whichWay=Vector3.Cross(transform.forward, normalizedDirection).y; //Return left or right?

```

To determine if its CW or CCW (ones positive, ones negative above)…

But after your post rozgo I have indeed found that the Vector.Angle is returning the same result… and I can use the cross product result to determine direction.

Cheers!

---

<div class="post-metadata">

### Author: ![Frido](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@Frido](https://discussions.unity.com/u/Frido)
#### Post date: [November 28, 2010, 9:14pm UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/8 "2010-11-28T21:14:38Z")

</div>

@blastone Thanks for the Vector.Cross, I was looking for some code to determine if a vector is positve or negative. Vector3.Cross did the trick. Thanks!

---

<div class="post-metadata">

### Author: ![Jack\_Wong](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@Jack\_Wong](https://discussions.unity.com/u/Jack_Wong)
#### Post date: [November 21, 2013, 6:52am UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/9 "2013-11-21T06:52:27Z")

</div>

Thank you! I am searching for this solution too! I am try to implement this to my ai so that it can decide steer left or right, Vector.Cross helps!

---

<div class="post-metadata">

### Author: ![makled](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/makled/32/87198_2.png) [@makled](https://discussions.unity.com/u/makled)
#### Post date: [August 10, 2021, 7:51pm UTC](https://discussions.unity.com/t/finding-the-angle-between-a-direction-and-a-point/398345/10 "2021-08-10T19:51:22Z")

</div>

> [@blastone](#):
>
> ```auto
> Vector3 targetDir =m_waypoint.transform.position - transform.position;
> targetDir = targetDir.normalized;
>       
> float dot = Vector3.Dot(targetDir, transform.forward);
> float angle = Mathf.Acos( dot ) * Mathf.Rad2Deg;
> 
> ```
> 
> This is what seems to work well for me,  
> the angle is always positive, so I also use a cross product
> 
> ```auto
> Vector3 normalizedDirection = m_waypoint.transform.position-transform.position;
>       
> float whichWay=Vector3.Cross(transform.forward, normalizedDirection).y; //Return left or right?
> 
> ```
> 
> To determine if its CW or CCW (ones positive, ones negative above)…
> 
> But after your post rozgo I have indeed found that the Vector.Angle is returning the same result… and I can use the cross product result to determine direction.
> 
> Cheers!

You can actually use Vector3.SignedAngle to get the angle and direction. Check it out [here](https://docs.unity3d.com/ScriptReference/Vector2.SignedAngle.html)
