- Home /

# Help with some math!

Hi guys!

Umm i have this Blend tree: ![alt text][1]

I want to make the red dot to be my enemy! The player is always facing towards the enemy, I used this code to get the angle between where the player is facing and the key press.

```
Vector3 direction = new Vector3 (iH.horizontal, 0, iH.vertical);
direction = Camera.main.transform.TransformDirection (direction);
direction.y = 0f;
float angle = Vector3.SignedAngle (transform.forward, direction, Vector3.up);
```

but i cant find the solution for making the red dot be where the enemy location is... I tried this:

```
Vector2 relativeToEnemy = new Vector2 (Mathf.Cos (temp), Mathf.Sin (temp));
```

but for some reason it has a weird behavior to it...

Please help me! I'm on this problem for 4 days!! [1]: /storage/temp/108569-צילום-מסך-20180104-ב210541.png

### People who like this

**Answer** by $$anonymous$$
·
Jan 04, 2018 at 07:53 PM

Sorry i don't understand which angle you want to measure exactly or at least that last part. However, if you have made sure the "angle" variable is calculated correctly, your "relativeToEnemy" vector shouldn't be far off. It's a pity your code doesn't show how your "angle" variable translates to your "temp" variable (and the naming isn't helping either). I guess you have already multiplied it with Mathf.Deg2Rad?angle between where the player is facing and the key press

Thanks for the reply! Oh that temp is a mistake - it waas only for testing!

This is how it supposed to look like: Vector3 direction = new Vector3 (iH.horizontal, 0, iH.vertical); direction = Camera.main.transform.TransformDirection (direction); direction.y = 0f;

```
float angle = Vector3.SignedAngle (transform.forward, direction, Vector3.up);
Vector2 relativeToEnemy = new Vector2 (Mathf.Cos (angle), Mathf.Sin (angle));
```

I am measuring the angle of: 'player.forward' (**my forward is always facing the enemy**) TO 'my input relative to world space'. But my angle is not the issue - it works great!

I cant find a way to make the angle translate to the blend tree.

Whats Mathf.Deg2Rad? and how should I use it?

!!!!

This worked! I dont realy understand what it did - if you can help me with that I would be grateful!

But neither way - Thank you!!

Final Code!

```
Vector3 direction = new Vector3 (iH.horizontal, 0, iH.vertical);
direction = Camera.main.transform.TransformDirection (direction);
direction.y = 0f;
float angle = Vector3.SignedAngle (transform.forward, direction, Vector3.up);
angle = angle * Mathf.Deg2Rad;
Vector2 relativeToEnemy = new Vector2 (Mathf.Cos (angle), Mathf.Sin (angle));
```

In mathematics we measure angles in radians. While most end user calculators which have trigonometric functions work with angles in degrees, almost all programming languages and in scientific context we always work with radians.

One full circle in Radians is `2*PI`

. It represents the length of the arc on the unit circle (circle with radius "1").

Most of Unity's API methods work with angles in degrees because they are easier to work with in user interfaces. So Quaternion.Euler, Quaternion.AngleAxis, ... all take angles in degree and Vector3.Angle, Vector3.SignedAngle return angles in degree.

However the pure mathematic / trigonometric functions (Sin. Cos, Tan, ...) always work with radians. To convert degrees to radians you just have to divide by 180 and multiply by PI. Therefore `Mathf.Deg2Rad`

is simply the constant `Mathf.PI / 180f`

. Likewise converting radians to degree is just the reverse. So `Mathf.Rad2Deg`

is just the constant `180f / Mathf.PI`

### Your answer

### Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. **Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.**

For more information, please read our full announcement.