# Angle Problem

**URL:** https://discussions.unity.com/t/angle-problem/417934
**Category:** Getting Started
**Created:** [July 12, 2010, 7:04pm UTC](https://discussions.unity.com/t/angle-problem/417934 "2010-07-12T19:04:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_1640392](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@anon\_1640392](https://discussions.unity.com/u/anon_1640392)
#### Post date: [July 12, 2010, 7:04pm UTC](https://discussions.unity.com/t/angle-problem/417934/1 "2010-07-12T19:04:28Z")

</div>

Hi all,

I’m working on a simple game, just started with Unity but i’m allready getting stuck. I’m trying out a top-down shooter in which the player allways points towards the mousecursor.

In actionscript flash I had it working, and I tried the same “method” in Unity, but it didn’t work at all,

> [@](#):
>
> Bx = transform.position.x;  
> Bz = transform.position.z;  
> Ax = Input.mousePosition.x;  
> Az = Input.mousePosition.y;
> 
> theZ = (Bz - Az) \* 1;  
> theX = Bx - Ax;
> 
> theAngle = Mathf.Tan(theZ/theX)/(Mathf.PI/180);
> 
> if(theX \< 0)  
> {  
> theAngle += 180;  
> }
> 
> if(theX \>= 0 theZ \< 0)  
> {  
> theAngle += 360;  
> }
> 
> transform.rotation.y = theAngle + 0;
> 
> Debug.Log(transform.rotation.y);

Now the first problem is that “theAngle” is jumping from 150 and about 600 when i move the mouse, while in flash this was just right, between 0 and 360.

The second is that “transform.rotation.y” always has the value of 1, which is weird, because “transform.rotation.y” is equal to “the angle”.

Also, if i put this code in my script:

> [@](#):
>
> function Update()  
> {  
> transform.rotation.y += 6;  
> }

Nothing happens,

Please help me out,

Greetz,

[/quote]

---

<div class="post-metadata">

### Author: ![j\_y\_k](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@j\_y\_k](https://discussions.unity.com/u/j_y_k)
#### Post date: [July 12, 2010, 10:19pm UTC](https://discussions.unity.com/t/angle-problem/417934/2 "2010-07-12T22:19:21Z")

</div>

I doubt that’s what you were doing in Flash. In any case, here’s your code with some comments:

```auto
Bx = transform.position.x; 
Bz = transform.position.z; 
Ax = Input.mousePosition.x; 
Az = Input.mousePosition.y; 

// Why multiply by 1 here?
theZ = (Bz - Az) * 1; 
theX = Bx - Ax; 

// You were probably using 'atan' (not 'tan') in your
// Flash code. In any case, the code from here...

theAngle = Mathf.Tan(theZ/theX)/(Mathf.PI/180); 

if(theX < 0) 
{ 
theAngle += 180; 
} 

if(theX >= 0 theZ < 0) 
{ 
theAngle += 360; 
}

// ...to here can (and should) be replaced with a call to
// 'atan2' instead, e.g.:

theAngle = Mathf.ATan2(theZ, theX)/(Mathf.PI/180);

// Note that you may have to tweak the input arguments and/or add
// some multiple of 90 to the result in order to get the desired effect.

// Also, what's the '+0' for?
transform.rotation.y = theAngle + 0; 

Debug.Log(transform.rotation.y);

```

---

<div class="post-metadata">

### Author: ![anon\_1640392](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@anon\_1640392](https://discussions.unity.com/u/anon_1640392)
#### Post date: [July 12, 2010, 11:56pm UTC](https://discussions.unity.com/t/angle-problem/417934/3 "2010-07-12T23:56:02Z")

</div>

Well, when it didn’t work, i went on, just screwing around to try to get it working, xD but I didn’t know i had to use atan2, thank you!
