- Home /
The question is answered, right answer was accepted
How do i get joystick angle and distance from its origin place?
tried this but not working:
var joystick : Joystick; //part of the script.
var V1 : Vector2;
var V2 : Vector2;
private var P : Transform;
function Start () {
V1 = new Vector2(joystick.position.x,joystick.position.y);
V2 = new Vector2(P.position.x,P.position.y);
}
function Update () {
P.position.y = joystick.position.y = 0;
P.position.x = joystick.position.x = 0;
var Di = Vector2.Distance(V1,V2);
var An = Vector2.Angle(V1,V2);
transform.rotation.y = An;
transform.position.z += Di;
}
Answer by wibble82 · May 05, 2014 at 09:33 PM
The magnitude of the position (joystick.position.magnitude) is effectively it's distance fom it's centre.
The angle can be calculated using the atan2 function: angle = Mathf.ATan2(pos.x,pos.y)
However that function gives you an angle in radians. To get it in degrees, multiply it by Mathf.Rad2Deg
The maths works! I'd need to see your exact code to know why it isn't doing the job. Or even any info at all! Help us help you basically - just saying "not working" doesn't help.
For anyone landing on this in 2021, the function is now Mathf.Atan2(x, y)
- Note that the 'T' in tan, is lowercased.
Answer by Jeff-Kesselman · May 05, 2014 at 09:01 PM
Use Input.GetRawAxis() to get number between -1 and positive 1 for each axis. Multiply that by the maximum deflection angle for your stick.
I saw that input.GetAxisRaw needs string variables, joystick has only floats , im making game for phones(android).