Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kbm · Mar 17, 2015 at 05:10 AM · mouse position

How can I move the mouse without moving the cursor? (Syringe Mechanic)

Hi everyone,

I have a Syringe in my game and the following behavior: The Syringe-Object follows the Mouse Position and when I click, I stop the Syringe from following the mouse and moving the mouse now presses the Plunger (which is part of / attached to the Syringe).

When I release the Mouse Button, unfortunately, the Syringe-Object makes a sudden jump because the mouse position changed. What I want, however, is to release the mouse button and have the Syringe stay at the same position as before.

How can I prevent this jump from happening? I already researched and found out that there is no native/cross-platform way to stop the cursor from moving.

Do you guys have any suggestions on how to move about?

Thanks for reading!

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
0

Answer by Bunny83 · Mar 17, 2015 at 05:22 AM

Like you already know there's no built-in (and therefore no cross platform) way to set the mouse cursor to a certain position besides the center (Cursor.lockState).

So if you need to set the mouse cursor to a certain position, you have to find a platform dependent way. For windows you have to use SetCursorPos (and maybe ClientToScreen) from the WinAPI. I don't work with other platforms (besides Android but it doesn't have a mouse ^^).

Of course there's no way to implement something like that in a webbuild.

edit
To implement a virtual mouse pointer all you need is to track the mouse delta and move a Vector3 around (could be a gameobject).

 // C#
 public float horizontalSpeed = 10f; // looks like "10" maps to the native speed
 public float verticalSpeed = 10f;
 
 public Transform cursorObj;
 
 public bool allowCursorMove = true;
 
 void CheckCursorLock()
 {
     if (Cursor.lockState == CursorLockMode.None)
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
 }
 
 void Start()
 {
     CheckCursorLock();
 }
 
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         CheckCursorLock();
     }
     float h = horizontalSpeed * Input.GetAxis("Mouse X");
     float v = verticalSpeed * Input.GetAxis("Mouse Y");
     Vector3 delta = new Vector3(h,v,0);
     if (allowCursorMove)
     {
         cursorObj.position += delta; // moves the virtual cursor
         // You need to clamp the position to be inside your wanted area here,
         // otherwise the cursor can go way off screen
     }
 }

This script will hide the hardware mouse cursor and instead moves a gameobject around according to the mouse delta. The gameobject could be anything, even a 3D object. Of course everything that involves a mouse position should use your virtual mouse position (the position of the cursor GameObject) from now on.

That could get a bit difficult with GUI stuff, but i think with the new UI system that should be doable. Just implement your own "PointerInputModule" derived module that doesn't use Input.mousePosition as the "StandaloneInputModule" does. Just use your virtual mouse position instead.

It's kind of impossible to use a virtual mouse position with the old GUI system (OnGUI).

To get the cursor right you have to consider the projection to the screen. The easiest would be to display an UI element in screen coordinates.

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 kbm · Mar 17, 2015 at 09:29 AM 0
Share

Thanks for your input. Unfortunately, I $$anonymous$$AY get that to work under windows but I also need it to work on $$anonymous$$ac & Linux so that's not a real option for me.

Do you know of any way to to "disable" the mouse while still tracking its x-axis / y-axis movement?

If there is absolutely no way to achieve that, I might have to implement the mechanic in a different way..

avatar image Bunny83 · Mar 19, 2015 at 02:27 PM 0
Share

@kbm: No, not with Unity integrated tools as the hardware cursor is under OS control. There is an OS API (at least in windows) to restrict the hardware cursor to a certain screen rectangle, but it's still just a platform specific solution.

The only solution that will work on all platforms is to not use the hardware cursor at all like @Firelight mentioned below. However using Input.mousePosition still uses the hardware mouse position.

You have to track your own position and use the mouse delta to move that virtual mouse around.

I'll add an example to my answer.

avatar image Sonoshee · Jul 12, 2015 at 02:05 PM 0
Share

The custom cursor that's supposed to imitate the mouse movement (using delta movement) indeed sticks with the mouse but only when it's moving at slow speeds. When I quickly move the mouse, the custom mouse and the system mouse are out of sync where the custom one is usually left behind. Any ideas?

avatar image Bunny83 · Jul 12, 2015 at 07:32 PM 0
Share

@Sonoshee: The virtual mouse is never in sync with the hardware cursor since the hardware cursor should be locked (clamped to the center) while you only use the virtual cursor. If some delta information is skipped there's not much you can do about that. You don't have direct access to the message pump of the OS when developing in Unity.

$$anonymous$$ouse events are generated asynchronously and are usually stored in a message queue. An application usually polls the message queue the OS provides in the main loop (called the message pump). The main loop usually passes those events to the appropriate module / control / event listener so they can process them. If you move the mouse quickly the OS will generate a lot "mouse move" messages in a short time. Unity has to process those and pass the information on.

$$anonymous$$eep in $$anonymous$$d that most game engines work with raw data. So if you have a mouse acceleration setup in your system settings, the hardware cursor will move more than the actual move delta the mouse produced. The mouse delta is the real movement of the mouse and does not include any smoothing, adaptive acceleration or virtual boundaries which are applied to the systems cursor (depending on the OS settings).

avatar image Sonoshee · Jul 12, 2015 at 08:28 PM 0
Share

Oh thanks a lot, this was really informative! I actually went with another strategy; since it's not possible to get mouse delta while the cursor is locked ( I mean lastPos - currentPos, not GetAxis), I left the cursor unlocked and whenever it exceeds a specific radius from the view center, I lock it and then unlock it so that it's reset to the view center. So far it's working really good. Thanks anyways for the clarification!

Show more comments
avatar image
0

Answer by Firelight · Mar 17, 2015 at 02:35 PM

Yes, you can !

You should hide the default cursor

Than create a virtual cursor, which is just a 2d sprite you show where you want, we will position the sprite based on the mouse position (which you can still get even if it's hidden)

You can than clamp the "virtual cursor" to the screen.

// Use this for initialization

     public GameObject cursor;
     public Camera c;
 
 void Start () {
         Screen.showCursor = false; 
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 mousePos = Input.mousePosition;
         Vector3 pos = c.ScreenToWorldPoint(mousePos);
 
         cursor.transform.position = pos;
     }






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 JanEricsson · Mar 31, 2015 at 03:08 PM

Since lockstate is disabled in Unity5 try this: http://answers.unity3d.com/questions/917677/center-cursor-and-toggle-on-and-off-unity-5.html

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 LoungeKatt · May 23, 2017 at 08:31 PM 0
Share

Screen.lockCursor is deprecated in Unity 5 and was replaced by Cursor.lockState, which in a way makes more sense because it is a cursor property.

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

7 People are following this question.

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

Related Questions

Lock Cursor to Screen resolution 1 Answer

mouse position seems to be the same everytime 1 Answer

Unity 2d Top-Down Mouse Aiming Stutters When Moving 2 Answers

Continuously rotating horizontally a player with the mouse position 1 Answer

Combine animation and mouse position 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