void RotateWeapon() { var pos = Camera.main.WorldToScreenPoint(transform.position); var dir = Input.mousePosition - pos; var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward); if (Mathf.Abs(angle) <= 90 && gunFlipped) { flipper.flipGun(); gunFlipped = !gunFlipped; } else if (Mathf.Abs(angle) > 90 && !gunFlipped) { flipper.flipGun(); gunFlipped = !gunFlipped; } }
// Use this for initialization void Start() { flipper = this.transform.Find("Gun").GetComponent <GunFlipper> (); var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position); var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; if (Mathf.Abs(angle) < 90) { gunFlipped = false; } else { flipper.flipGun(); gunFlipped = true; } }