- Home /
Force update of mouse position with the new input system
I am using the new Input system to move my mouse with Mouse.current.warpCursorPosition()
but Mouse.current.position.ReadValue()
seems to not update unless the mouse is physically moved again (or a mouse button is pressed). Is there any way to force the input system to update the mouse position via code directly after calling warpCursorPosition()
?
Answer by JonnyD · Jul 24, 2020 at 03:04 PM
You should try InputState.Change()
to set the cached state of the mouse, e.g.: InputState.Change(Mouse.current.position, warpedPosition);
This is exactly what I needed! I've been trying for hours to figure out how to manually set this in one of my tests to make it more dynamic! Thank you!!!
Big thanks also from me, really needed this!
For anybody stumbling across this and not being able to use the InputState.Change() function right away: You gotta put "using UnityEngine.InputSystem.LowLevel;" on top of your script.
Visual studio doesn't seem to recognise "UnityEngine.InputSystem.LowLevel;", do you know if maybe they changed it in the last version of unity to a different package name?
You need install InputSystem from Unity Package Manager
Answer by jhughes2112 · Sep 23, 2021 at 12:31 AM
// Force the mouse to be in the middle of the screen
Vector2 warpPosition = Screen.safeArea.center; // never let it move
Mouse.current.WarpCursorPosition(warpPosition);
InputState.Change(Mouse.current.position, warpPosition);
Putting this together from the above comments, this block forces the mouse cursor to the center of your play area, even while in the Editor. Thanks!
One thing I have noticed about this is, immediately after you warp the mouse cursor position (specifically with InputState.Change), if you are moving slowly to the RIGHT, the delta comes back slightly LEFT, both in the Editor and in a built client. There's some rounding error happening, which is maddening.
What I posted above works perfectly in editor or built client. The issue I mentioned is a bug (or series of them) in the new Input system when fetching mouse move deltas. The new system fails to account for warping properly, so I ended up going back to the old input system just for mouse movements, with GetAxis("Mouse X"). Works perfectly even when warping the cursor.
Answer by AlienFreak · Nov 10, 2021 at 11:56 AM
I came up with a very legit way to move the mouse cursor and another to force a mouse click event because Unity is so annoying in not letting you do this when using Cursor.lockState. Now you can set Cursor.lockState and/or Cursor.visibility in Start() and call either of the following below to force the mouse to behave. (Verified working in macOS Big Sur, Unity 2021.1.17)
Stupid easy way to force mouse cursor position to center of game window in editor only from code:
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
public static void ForceMousePositionToCenterOfGameWindow()
{
#if UNITY_EDITOR
// Force the mouse to be in the middle of the game screen
var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
Vector2 warpPosition = game.rootVisualElement.contentRect.center; // never let it move
Mouse.current.WarpCursorPosition(warpPosition);
InputState.Change(Mouse.current.position, warpPosition);
#endif
}
Stupid easy way to force click in game window in editor only from code:
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
public static void ForceClickMouseButtonInCenterOfGameWindow()
{
#if UNITY_EDITOR
var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
Vector2 gameWindowCenter = game.rootVisualElement.contentRect.center;
Event leftClickDown = new Event();
leftClickDown.button = 0;
leftClickDown.clickCount = 1;
leftClickDown.type = EventType.MouseDown;
leftClickDown.mousePosition = gameWindow;
game.SendEvent(leftClickDown);
#endif
}
Looking at your code this will only work in the editor. So ok I can do that while developing but I can't actually use it inside game, because the code will be stripped when building
That SHOULD only be necessary as editor code to grab the Game window. (Haven't verified)
The InputState.Change should take care of moving mouse position instantly in the first routine as per the OP question and the Event / SendEvent in the second routine will handle mouse clicking (you can do many other mouse states events just by changing the event type.)
Note: I used this code for the purpose of getting the mouse to click in the game window automatically on start. This is necessary for Cursor lock and visibility to function as expected in the editor. The Cursor lock/visibilty function completely normally and as expected in any build without any of this code.
You can just use Screen.safeArea.center to get the center of the game view, which doesn't require digging into assemblies.
Your answer

Follow this Question
Related Questions
Input.mousePosition Dead Zone - See gameplay 0 Answers
Ray Over Mouse 1 Answer
Can I fake the mouse/touch position relative to the real mouse/touch position? 0 Answers
Mouse Input Lag 3 Answers
Input System Can't Catch Event on Update 0 Answers