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 Ilja Grach · Dec 24, 2009 at 08:47 AM · mouseoafa

Set cursor position

How can i set mouse cursor position?

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

6 Replies

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

Answer by Ashkan_gc · Dec 24, 2009 at 01:42 PM

in mono you can use the system.windows.form.cursor.position but it needs the windows.form.dll and you need to add this assembly to your project. for mouse clicks you should use external APIs witch are in user32.dll but i don't know anything about OSX. in web players you can use javascript to do mouse movement and click.

note: you can not use external native code in web players.

Comment
Add comment · Show 5 · 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 michael 4 · May 17, 2011 at 08:58 PM 0
Share

how do you add this assembly to your project? I referenced it in mono, but it keeps dereferancing when I compile in unity?

avatar image Xtro · Jul 31, 2013 at 07:29 PM 0
Share

You can't modify a generated file. The mono solution is generated by Unity.

You can add the dll file into your Unity asset folder and write "using namespaceOfTheDll;" at the beginning of your script to be able to use it.

avatar image Rajatrc · Jul 17, 2016 at 08:04 AM 0
Share

$$anonymous$$ine is a $$anonymous$$oving UI, So it is must for me to reset cursor to centre of screen every time user hits a button or returns to $$anonymous$$ain $$anonymous$$enu. So plzz help.

avatar image Le-Pampelmuse Rajatrc · Jul 18, 2016 at 02:33 AM 1
Share

If you need help with anything, then DON'T ask for it in a comment to a question that is 7 years old.

Please be sure to read the user guide and watch this video:

https://www.youtube.com/watch?v=ezAPpViLs2Q

If you need help, google your question and if you can't find an answer, ask a new question. Properly formulate your problem and explain what you tried and what doesn't work.

Don't do that here, ask a new question. But only if you can't find an answer on the internet.

avatar image Calos1591 · Aug 13, 2016 at 10:23 PM 0
Share

problem is you also have to add System.Drawing in order the Point method to change System.Windows.Form.Cursor.Position.

avatar image
5

Answer by Benproductions1 · Jul 04, 2012 at 10:33 AM

You can work around this problem, by basically manually implementing a GUI based mouse and moving it through the mouse delta given by Unity. You could then set the position of this mouse and just use static variables to access it from every script.

Wish unity would fix it, so we don't have to do these stupid workarounds.

Comment
Add comment · Show 1 · 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 Sushi271 · Mar 22, 2014 at 07:20 PM 0
Share

I don't see this. Where can I get this mouse delta? Assu$$anonymous$$g it's Input.mousePosition from this and previous frame subtracted, then what, when real mouse will touch the screen edge? All deltas will become 0, even if GUI cursor will be on the center of the screen.

Edit: Oh, ok. I can do this with an axis.

avatar image
2

Answer by duck · Dec 24, 2009 at 10:10 AM

I don't think it's possible to directly set the mouse cursor position with Unity's own API (although it's likely possible if you use a custom DLL in a standalone build).

However, if your goal is to set the mouse cursor position in order to achieve a "mouse look" effect, there's a lockCursor command to do just this.

Comment
Add comment · Show 1 · 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 New_Game_Ideas · May 24 at 03:05 PM 0
Share

Is there a way to do it on WebGl build? [DllImport("user32.dll")] is not working on WebGl build.

avatar image
2

Answer by amisane · Nov 01, 2016 at 04:32 PM

I was able to create an implementation for this that works (in my 2D game running on Windows at least...) Add this struct in to your project:

 [StructLayout(LayoutKind.Sequential)]
 public struct Point
 {
     public int X;
     public int Y;
     public static implicit operator Vector2(Point p)
     {
         return new Vector2(p.X, p.Y);
     }
 }

And add the following to some script:

      [DllImport("user32.dll")]
         public static extern bool SetCursorPos(int X, int Y);
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool GetCursorPos(out Point pos);
     
         private void MoveCursorToNearbyObject(GameObject objToFocus)
         {
             float targetWidth = 1920f;
             float targetHeight = 1080f;
     
             Vector2 inputCursor = Input.mousePosition;
             inputCursor.y = Screen.height - 1 - inputCursor.y;
             Point p;
             GetCursorPos(out p);
             var renderRegionOffset = p - inputCursor;
             var renderRegionScale = new Vector2(targetWidth / Screen.width, targetHeight / Screen.height);
     
             var objPos = objToFocus.transform.position;
     
             var newXPos = (int)(Camera.main.WorldToScreenPoint(objPos).x + renderRegionOffset.x);
             var newYPos = (int)(Screen.height - (Camera.main.WorldToScreenPoint(objPos).y) + renderRegionOffset.y);
     
             SetCursorPos(newXPos, newYPos);
         }
 

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 ruudvangaal · Aug 01, 2017 at 09:30 AM 0
Share

I'm getting:

The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)

when I try the above; do I need to add a 'using'?

avatar image ruudvangaal ruudvangaal · Aug 01, 2017 at 09:33 AM 0
Share

Duh, ignore that. It was just 'using System.Runtime.InteropServices;'.

avatar image
0

Answer by NightmarexGR · Apr 30, 2014 at 11:16 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 · 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
  • 1
  • 2
  • ›

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

12 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

Related Questions

Can't disable MouseLook Y axis in pause screen 1 Answer

How to move a sound knob (Gui Texture) around a pivot with touch and mouse 1 Answer

Mouse Orbit script following other object 0 Answers

Having problems getting a zoom to mouse function working well with smoothstep, need some help. 0 Answers

[Solved]Cursor emulation with canvas? 0 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