Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by krontobi1 · Jan 12, 2018 at 10:33 AM · lockcursor

How to lock the cursor without moving to the center? (С#)

HELP PLS!!! (С#) I know how to lock the cursor, but it moves to the center, but I do not need it, I need it from the same place where I clicked.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jan 12, 2018 at 11:45 AM

Unity doesn't support this because just locking in place can cause problems. Locking the cursor requires the engine to manually set the hardware cursor to the locking position every frame. However since the hardware cursor is moved asynchronously by the OS the cursor still moves between two frames. If you lock the cursor close to the game window edge it would be possible that the cursor actually leaves the game window area. If the user clicks at that moment your game will loose focus as the OS sees a click outside the window. That's the main reason why we lock to the screen center to have enough movement space in all directions.


Unity does have an alternative lock state but only for standalong targets which is CursorLockMode.Confined. Confined does not lock the cursor in place but uses functionality provided by the OS to confine the hardware cursor to a rectangle on screen. On Windows systems this is done by using the ClipCursor method.


If your target is Windows standalone you can of course manually lock the cursor to a specific position by using the Win API method SetCursorPos. However you would have the same issues i mentioned at the beginning. Usually you don't care about where you lock the cursor but you only care about where the cursor is located after you unlock the cursor. For windows standalong i would recommend to simply use Unity's normal cursor locking but in addition before locking the cursor you would remember the last cursor position (GetCursorPos) and after you released the cursor you would use SetCursorPos to move the cursor back to it's last position.

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 krontobi1 · Jan 12, 2018 at 02:08 PM 0
Share

thank) I use this link text Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);

avatar image Bunny83 krontobi1 · Jan 12, 2018 at 11:49 PM 0
Share

I wouldn't recommend to use System.Windows.Forms.dll. You would need to include that dll in your project just to set the cursor position. The Cursor class inside the Windows.Forms dll just wraps the Windows API function i mentioned above. The definition of GetCursorPos and SetCursorPos can be found on any pinvoke site.

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

74 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

Related Questions

Screen.lockCursor for web player? 2 Answers

How do I lock and then unlock the cursor using the same key? 2 Answers

Can you lock the cursor to specific coordinates? 0 Answers

screen.lockcursor locks the cursor in wrong place 1 Answer

Can't enable Cursor 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