- Home /
Aimable reticle using mouse which has limited degrees
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);
Answer by E_101 · Jan 24, 2018 at 05:23 AM
Not sure if this will help, but if you use, gameObject.transform.rotation.eulerAngles.z <= 90)
. The "z" designates the axis you want to check for angle height, so you can make it y , x, or z. If you use this with an "if" statement you can make sure that the cursor only goes to the desired height. I am sorry I do not have all the code for you but hopefully this will suffice. Best of luck!
Oh, also! You may have to assign some sort of "aimer" Game Object for the gameObject.transform , such as an indicator sprite for the angle; maybe even the actual cannon. This works for 2D not sure if you are doing 3D or not but it should be fairly similar. Again, best of luck!
This doesn't really solve the problem with my current implementation. However, I think I'm going to just make it so the ship just fires straight forward or something, unless I can find an easy way to calculate what the pixel radius should be for a given max degrees.
Wish I could have helped more! Hopefully someone will be able to help you out and finally solve this!
Your answer
Follow this Question
Related Questions
Problem With Player Looking At Mouse Position 0 Answers
C# Track Mouse Cursor Movement Speed 2 Answers
How to start Animation left turn With the cursor 1 Answer
Multiple Cars not working 1 Answer
Left Click Script Problems 1 Answer