I am using a virtual joystick as aim for my top down shooter game and my character refuses to turn more than 180 degrees. When the rotation exceeds 180 it turns back towards 0 even though the rotation value still increase towards 360.
Here is my code:
void LateUpdate () {
Vector2 toVector = JSB.position - JS.position;
float angelToTarget = Vector2.Angle (new Vector2 (0, -1), -toVector);
Debug.Log (angelToTarget);
Debug.Log (new Vector2 (0, 1));
//float horizontal = -Input.GetAxis("Horizontal") * Time.deltaTime * playerSpeed;
//float vertical = Input.GetAxis("Vertical") * Time.deltaTime * playerSpeed;
// Disabled for testing
if (SystemInfo.deviceType != DeviceType.Desktop) {
if (GameObject.FindGameObjectWithTag ("GameController").GetComponent<ShopOpen> ().SOpen) {
var mouse = Input.mousePosition;
var screenPoint = Camera.main.WorldToScreenPoint (transform.localPosition);
var offset = new Vector2 (mouse.x - screenPoint.x, mouse.y - screenPoint.y);
var angle = Mathf.Atan2 (offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (0, 0, angle);
}
//Disabled for testing
} else if (SystemInfo.deviceType != DeviceType.Handheld) {
if (angelToTarget != 90) {
gameObject.transform.rotation = Quaternion.Euler (0, 0, angelToTarget);
} else {
gameObject.transform.rotation = Quaternion.Euler (0, 0, 90);
}
}
}
I am getting the angel from the joystick using the vector 2 in the beginning and i am trying to set the rotation on my character to the same.