Joystick Angle Problem
Hello, I am trying to use a controller's joystick and get the angle the joystick is at. I currently have this code:
angle = Mathf.Atan2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical")) * Mathf.Rad2Deg;
Which works fine until you release the joystick which returns the angle to zero (to be expected). My question is however, how can I save the previous angle and keep it when the joystick is released? Ive tried this:
if (stickInput.magnitude > deadZone)
{
angle = Mathf.Atan2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical")) * Mathf.Rad2Deg;
}
Which works fine until the joystick's angle is negative. (Since the angle is calculated counter clockwise from 0 to 180 then -180 to 0) When it is, every so often(after numerous tests it seems completely random) the angle will become positive when the joystick is released instead of staying negative. For example, I turn the joystick to the left completely. The angle is -90. Sometimes when the joystick is released, the angle will become 90.
If anyone knows why this is happening or how to fix it, please let me know. If my explanation didn't make sense, I'll try to reword it.
Thanks for the help.