- Home /

# How to get the angle between an object and a plane based on the plane's normal?

I have an gun which raycasts onto a plane and I want to find the angle between the plane and the object based on the normal of the raycast hit. Also, I want to isolate the pitch and yaw seperately.

Currently I use this approach to find the pitch and yaw separately but it is not relative to the normal.

```
Vector3 targetDir=raycastHitPos - gunNozzle.transform.position;
float pitchAngle = Vector3.SignedAngle(new Vector3(0, targetDir.y, 0),gunNozzle.transform.forward, gunNozzle.transform.right);
float yawAngle = Vector3.SignedAngle(new Vector3(targetDir.x, 0, 0), gunNozzle.transform.forward, gunNozzle.transform.up);
```

I tried

```
Vector3 targetDir = Vector3.Cross(gunNozzle.transform.forward.normalized,normal.normalized);
```

but it doesn't work :/. How do I fit the raycast hitpoint normal into this code? Fairly new to Vector math so please guide me on how to achieve this! Thank You!

**EDIT 1:**

**This image shows my current implementation and what I need**

The yellow arrow is the facing direction and the green arrow is the normal of the red plane. These are the values which I get when facing exactly opposite to the normal. The second image shows the values which I want. I want the same angles whichever way I face the plane(or the plane is facing). The values are only for representation purposes, I'm fine with any range!

*I've also tried*

Reflecting the vector and then trying to find the angle between the hitpoint and the reflected vector but it only gives a value of either 180 or 0.

*I also want to use Vector3.SingedAngle() because I want negative values!*

**Answer** by Vivekveri
·
Mar 27, 2018 at 01:29 PM

I Found the solution, it's only for pitch currently, will update it once I get the Yaw sorted out! I figured it would be easier to find the angle between the normal and the gun's nozzle but with only y and z values (since I didn't want to take yaw into account) and surprisingly, it works!

**I shall accept this as an answer and will get back if i face problems with yaw**

This is the code for the pitch only angle

```
float pitchAngle = Vector3.SignedAngle(normal, new Vector3(0, gunNozzle.transform.forward.y, gunNozzle.transform.forward.z), gunNozzle.transform.right);
```

Thank you for everyone who answered, commented and saw this question!

**Answer** by tormentoarmagedoom
·
Mar 26, 2018 at 08:12 AM

Good day.

I just did the same to create a "compass". I used the function Vector3.Angle , you need two vectors, and you get the angle between them. Think about which vector defines your plane (0,1,1), or (1,0,1) or (1,1,0). Check the API and sure you will get it!

```
If helped, accept the answer! :D
```

@tormentoarmagedoom - Thank you for the reply, but I want to use SignedAngle() because I need negative values! Your answer did help me to try out something new, but it did not work for me :/, Sorry.

Ah yea is true! it only gives positive angles from 1 to 180.

I had to create a system to know if was to right or to left. By creating the vector between the two original vectors, i can know in wich direction is (top-right, top-left, bot-right or bot-left)

```
OriginalVector1-OriginalVector2 = DiferenceVector.
```

If DiferenceVector.x >0 means is top, if < 0 means is bot If DiferenceVector.y >0 means is right, if <0 means is left.

Thats what i did :D

**Answer** by Firas4d
·
Mar 26, 2018 at 03:07 PM

The cross operation you did will always result in a third vector that is perpendicular to both operand vectors and parallel to the plane surface which will later play a big role deciding the sign of the angle if you used it as a third parameter inside SignedAngle method as following : Vector3.SignedAngle(objectForwardVector, planeNormal, crossResult);

So if we applied that on your example image then you will get the signed angle between the green and yellow arrows which will be 180 or -180 degrees (depends on the direction of the cross product result) and will never be 90, but at least you will get a result that is always relative to the plane normal.

Thank you! it does work, but not quite how I wanted it, I found a go-to solution! Thank you! :)

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