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
0
Question by THEpancakes · Oct 10, 2012 at 09:04 PM · guipositionmousecursor

Setting the mouse position to specific coordinates

Hi,

I am making a game that involves the OnMouseOver() function, and I would like the game to start with the player's cursor already positioned at specific coordinates, but with the player will still being able to move his cursor after that. Is this possible? I know how to lock the cursor in the middle of the screen, but how could I manually set its position at the beginning of the game?

Thanks in advance. :)

Comment
Add comment · Show 1
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 sparkzbarca · Oct 10, 2012 at 09:13 PM 0
Share

You can use the GUI to create a mouse cursor, similar to creating a crosshair and manipulate that image.

You cannot however move the cursor itself unless you include windows.form.dll.

Basically the cursor is a windows operating system object that Unity reads information from. It doesn't write to it though, its not a unity object. You'd need to basically include microsoft's code for the mouse to move the mouse, hence windows.form.dll

5 Replies

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

Answer by skalev · Oct 10, 2012 at 09:14 PM

You can't. This is controlled by the operating system. (as far as I know). How do you lock the cursor in the middle of the screen? if you set it somehow, just set it to the right coordinates.

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 sparkzbarca · Oct 10, 2012 at 09:18 PM 0
Share

screen.lockcursor = true;

avatar image skalev · Oct 10, 2012 at 09:34 PM 0
Share

What you can do though, is HIDE the cursor and then write your own class that shows a different cursor (using an Image you created) and moves along with the mouse. However, this isn't a great solution, since you will always have an offset between your mouse and the real mouse, which means yo cannot use any of the built in On$$anonymous$$ouse functions, and will have to implement those yourself. Probably not worth the trouble...

avatar image
36

Answer by zachwuzhere · Mar 03, 2018 at 09:47 PM

Anyone who still needs to move the move position, here is one method...

    //C#
    using System.Runtime.InteropServices;
    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    int xPos = 30, yPos = 1000;   

    SetCursorPos(xPos,yPos);//Call this when you want to set the mouse position
         

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 cobykaufer · Aug 31, 2018 at 04:28 AM 0
Share

don't know why this doesn't have more upvotes since it seems to solves the problem perfectly.. does this work accross diferent platforms somehow?

avatar image HarvesteR cobykaufer · Jun 27, 2019 at 05:34 PM 2
Share

It doesn't. user32.dll is a windows library, so this fix is windows-only.

Other OSes probably have similar system calls to do this sort of thing though.

avatar image $$anonymous$$ · Jul 11, 2019 at 10:45 PM 0
Share

This works to perfection!

avatar image cobykaufer · Jul 12, 2019 at 06:40 AM 0
Share

Now if only we can get this to work for mac, linux (and webGL)

avatar image $$anonymous$$ · Jul 12, 2019 at 12:56 PM 0
Share

I saw someone else on another post using Hardware.mouseposition so maybe that would work on other platforms.

avatar image jinoh · Mar 06, 2020 at 10:37 PM 0
Share

THank you very much, you saved my time a lot!

avatar image
2

Answer by elmckdaddy · Dec 05, 2020 at 07:17 AM

I know this is super old, but I found this question tonight, so maybe someone else will land here as well. If you're looking for how to set the cursor position using the new input system, check out this answer for a cross platform solution.

Comment
Add comment · 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
1

Answer by sparkzbarca · Oct 10, 2012 at 09:34 PM

You can use the GUI to create a mouse cursor, similar to creating a crosshair and manipulate that image.

You cannot however move the cursor itself unless you include windows.form.dll.

Basically the cursor is a windows operating system object that Unity reads information from. It doesn't write to it though, its not a unity object. You'd need to basically include microsoft's code for the mouse to move the mouse, hence windows.form.dll

Comment
Add comment · 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
0

Answer by NightmarexGR · Apr 30, 2014 at 11:15 AM

Hello guys it seems this problem is still active, for this reason i created a unity API that lets you set the cursor's position on screen "SetCursorPosition(x : int,y : int)"

http://forum.unity3d.com/threads/242832-Official-Set-Cursor-Position?p=1606714#post1606714

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 _gord0_ · Apr 01, 2017 at 04:09 PM 1
Share

I know this thread is 3 years old, but I've seen you paste this comment on a bunch of related threads. Your workaround is for Windows only and uses js ins$$anonymous$$d of c#. Both of which are inadvisable. Even if it were multiplat, one would first be faced with translating to c#.

avatar image NightmarexGR · Jan 28 at 04:28 AM 0
Share

5 years later, the asset is a paid solution for windows only but has great support and works with c# just fine https://assetstore.unity.com/packages/tools/input-management/set-cursor-position-mouse-clicks-api-17177?aid=1101liJWIxCr

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

18 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

Related Questions

Locking cursor without changing position 1 Answer

custom mouse changing when hovering over objects 1 Answer

Disable/enable script and animation when you move your mouse cursor 1 Answer

Use OnMouseEnter/OnMouse Exit with center of screen 1 Answer

get the mouse cursor position x and y and make gui follow on trigger -1 Answers


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