Angle Problem

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,

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:

Nothing happens,

Please help me out,

Greetz,

[/quote]

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

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);

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!