# Slowing down mouse movement in Unity

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...)

• 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? Jan 10 at 14:12
• @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! Jan 10 at 15:14
• @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 Jan 10 at 16:24
• 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. Jan 10 at 16:33

1. Make the regular hardware mouse cursor disappear with Cursor.visible = false; in the Start method of some script.
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.
    transform.position = new Vector3(

• 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! Jan 10 at 19:11