• 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 (w$$anonymous$$ch 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 t$$anonymous$$s 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 somet$$anonymous$$ng 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
     }
 }

T$$anonymous$$s script will $$anonymous$$de the hardware mouse cursor and instead moves a gameobject around according to the mouse delta. The gameobject could be anyt$$anonymous$$ng, even a 3D object. Of course everyt$$anonymous$$ng 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 t$$anonymous$$nk 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
avatar image Bunny83 · Mar 19, 2015 at 02:27 PM 0
Share
avatar image Sonoshee · Jul 12, 2015 at 02:05 PM 0
Share
avatar image Bunny83 · Jul 12, 2015 at 07:32 PM 0
Share
avatar image Sonoshee · Jul 12, 2015 at 08:28 PM 0
Share
Show more comments
avatar image
0

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

Yes, you can !

You should $$anonymous$$de the default cursor

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

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

// Use t$$anonymous$$s 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 t$$anonymous$$s: 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Directional mouse movement independent from position? 1 Answer

How to project the mouse cursor into 3D space for LookAt? 1 Answer

Raycast onto Mesh Collider unreliable. 0 Answers

Player should move to cursor 0 Answers

Axis : top down shooter help 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