Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by Fuzzel_ · Jun 03, 2020 at 11:21 PM · inputmousemouseposition

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()?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
6

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elmckdaddy · Dec 05, 2020 at 07:12 AM 1
Share

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!!!

avatar image Mokofr · Jun 26, 2021 at 01:23 PM 3
Share

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.

avatar image nadavelmaliah Mokofr · Jan 13 at 10:07 AM 0
Share

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?

avatar image jo_slim nadavelmaliah · Jan 26 at 01:16 PM 0
Share

You need install InputSystem from Unity Package Manager

avatar image
1

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jhughes2112 · Oct 14, 2021 at 07:55 PM 0
Share

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.

avatar image jhughes2112 · Nov 10, 2021 at 03:14 PM 0
Share

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.

avatar image
0

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
     }
 

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fuzzel_ · Nov 10, 2021 at 02:47 PM 0
Share

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

avatar image AlienFreak Fuzzel_ · Nov 11, 2021 at 02:44 AM 0
Share

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.

avatar image jhughes2112 AlienFreak · Nov 11, 2021 at 02:50 AM 0
Share

You can just use Screen.safeArea.center to get the center of the game view, which doesn't require digging into assemblies.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

167 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Touch inputs on a Desktop type device 2 Answers

Baffling Input.Mouseposition problem. If statement not correctly working. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges