I'm using the game to control electronics. I use the following code to determine the location of an object (the location is used to perform calculations that are sent to the electronics)

int centerX = Screen.width/2;
int centerY = Screen.height/2;
Vector3 centerVec = new Vector3(centerX, centerY);
Vector3 diffFromCenter = Input.mousePosition - centerVec;
Ray ray = Camera.main.ScreenPointToRay(centerVec + 0.5F*diffFromCenter);

This is used (along with a raycast to determine the appropriate y value) to determine the location of the object.

The reason I use 0.5*diffFromCenter is because of the sensitivity of the movement (as seen in the reaction of the electronics)

The problem with this approach is that it when the (real) cursor reaches the edge of the screen and can't move further, the in-game object won't be able reach the edge of the game window.

The only idea I could come up with to solve this problem is to use code to change settings on the speed of the mouse's movement.

I can't come up with a better solution (and this one would require that I also detect when the cursor leaves the game window to restore the mouse movement speed, because we'll be running experiments and someone might need to tweak things during the experiment...)

  • 1
    \$\begingroup\$ Presumably you considered setting Cursor.lockState to Locked or Confined to keep the mouse from exiting the window, and reading Input.GetAxis("MouseX") (and "MouseY" respectively) to get the relative change in position of the mouse, which you can then scale and accumulate as you desire? \$\endgroup\$
    – DMGregory
    Jan 10, 2022 at 14:12
  • \$\begingroup\$ @DMGregory Preventing the mouse from exiting the window wasn't a problem for me. I haven't tried Input.GetAxis - I've come across this but it seems I misunderstood what it does. reading it again, I think I understand how I can use it. I'll give that a try. Thank you! \$\endgroup\$ Jan 10, 2022 at 15:14
  • \$\begingroup\$ @DMGregory tested and works. Thank you. if you want to post this as an answer, i'll accept it. Seems my misunderstanding of the documentation was why I failed to solve it myself \$\endgroup\$ Jan 10, 2022 at 16:24
  • 1
    \$\begingroup\$ I won't be free to post an answer today, but I think it would be a good idea to write up your own answer showing how you solved it, so others can learn from your experience. \$\endgroup\$
    – DMGregory
    Jan 10, 2022 at 16:33

1 Answer 1

  1. Make the regular hardware mouse cursor disappear with Cursor.visible = false; in the Start method of some script.
  2. Create a new game object called "Mouse Cursor". I recommend using an image on a screen-space canvas for this purpose, but in a 2d game you could use a sprite in world-space as well. Give it a pointy-looking sprite asset which goes well with the aesthetics of your game UI.
  3. Create a script on it which uses Input.GetAxis("Vertical") and Input.GetAxis("Horizontal") to measure the vertical and horizontal mouse movement since the last update. Multiply that value with Time.deltaTime and your desired speed multiplier, and change the position of the mouse cursor object accordingly.
  4. You might notice that it is now possible for your new custom mouse cursor to move outside of the screen. So you need to make sure to keep it within the screen boundary. You can do that with:
    transform.position = new Vector3(
         Mathf.clamp(transform.position.x, 0f, Screen.width),
         Mathf.clamp(transform.position.y, 0f, Screen.height),
  1. Now you might want to know how to find out if and which collider the mouse pointer is pointing at.
  • \$\begingroup\$ I didn't end up creating a new mouse cursor, since I don't need a visible cursor. Using Input.GetAxis for the mouse X and Y directions was enough (I previously encountered that solution but misunderstood what GetAxis returns). Even though someone already commented before you with the suggestion of using GetAxis, I'll be using the clamping code you provided. Thank you! \$\endgroup\$ Jan 10, 2022 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .