I’m basically making something for aiming both starfighter rotation and cannon facing direction. I want it to be limited to, say, 10 degrees no matter your screen resolution. My current implementation gives you a wider firing range the lower your screen size. For ship rotation, I simply divided by 100 (the max pixel radius) but for cannon firing, this will not fire in the expected direction because the reticle may be farther than it actually fires. Is there a way to calculate what the pixel radius should be based on the resolution? Or maybe an even better way to do the whole thing?
int centerX = Screen.width / 2;
int centerY = Screen.height / 2;
var center = new Vector3(centerX, centerY, 0);
var mouse = Input.mousePosition;
if (mouse != _lastMousePosition && _joystick)
{
_lastMousePosition = mouse;
_joystick = false;
}
if (Input.GetAxis("Joystick Y") != 0 || Input.GetAxis("Joystick X") != 0) _joystick = true;
if (_joystick)
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.None;
_offset.x = Input.GetAxis("Joystick X") * 100;
_offset.y = Input.GetAxis("Joystick Y") * 100;
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
_offset = mouse - center;
if (Vector3.Distance(_offset, Vector3.zero) < 10) _offset = Vector3.zero;
}
if (Vector3.Distance(_offset, Vector3.zero) > 100) _offset = _offset.normalized * 100;
var direction = Camera.main.ScreenPointToRay(_offset + center).direction;
var directionRotation = Quaternion.LookRotation(direction);