- Home /
Glue mouse to object
Hey everyone, is it possible to fixate a cursor to certain XY-coordinates, as long as it remains in a specified area? I want to "jump" from the center of one game object to the center of another game object when a XY-threshold is reached. Do you have any suggestions? Regards!
I haven't done anything like this but you can gain access to the cursor position through the .Net api from there it is only a matter of setting up the logical statements for control how you want to be able to move the mouse.
If I understand correctly you want the mouse locked to one position but when they attempt to move a certain speed or a certain distance you want it to snap to the next game object?
If this is true it would seem that you will run into a problem since keeping the mouse snapped to one position you will effectively stop any input from it so it will not ever be in motion away from the original target.
What are you trying to accomplish with having the mouse stay locked to one target? $$anonymous$$aybe we can help with a solution that address with that issue.
Basically it's about gesture-input. To compensate for the inaccuracy of the finger gesture, I want to snap the cursor to the nearest neighbour. I understand the problem you described, but Dracorat mentioned it was possible to decouple a software cursor from mouse input..
I was thinking it might be along those lines but I wasn't sure. The basic logical outline I would go for would be some thing like.
Lock the mouse to a target the first target or starting position in the on start statement. the release the mouse so it can be dragged.
Here's where the trick will be you will need to grab the direction the mouse is going as it's being dragged. On$$anonymous$$ouseUp you will do a raycast in that direction to return the closest item. You may want to do a ray cast of all items in a certain arc and return the closest one. The arc will compensate for if the user is just a little off of the target.
Then when the transform gets returned from the new target in the direction they were moving the mouse you can check whether that is X distance (X being the threshold) from the last locked targets transform. If it was greater than X then they passed the threshold and the mouse would get snapped to the new targets transform position. If the threshold was not exceeded the mouse would snap back to the original target.
In this setup the cursor actually moves but snaps either to a new target or back to the old one.
If you really want the mouse to appear not to move you should be able to just draw a custom image of the cursor at the Currently Locked position and then just use the CursorVisible from .net to make the real mouse cursor invisible and then do the same thing I was just explaining above with raycast. The only difference being that when you snap the mouse to the new position you would also draw your custom cursor at the new position and destroy the one at the old location.
If you are leaving the real cursor invisible you would still want to have it snap to the new or back to the old position to give the user the appropriate point of reference where to move the mouse from and keep from breaking the raycast.
Answer by greatwhiteshark17283 · Apr 17, 2013 at 04:54 PM
If you are using the First Person Controller, make the camera a parent of the object.
Answer by Dracorat · Apr 17, 2013 at 05:08 PM
You'll have to:
Use a software mouse cursor (required). You'll use a blank image so that nothing is actually drawn.
Decouple the position of the cursor from the position of the mouse.
Draw the mouse cursor manually (with something like a GUI.DrawTexture).
In short, you'll do a raycast each frame. If the raycast "hits" on of your desired snap points (you can use a sphere collider if you like) then you draw the cursor on the object hit. If it doesn't hit a collider, you draw the cursor at the location of the mouse.
Your answer
Follow this Question
Related Questions
Folow mouse script 0 Answers
How To Check Which Side of The Screen Your Cursor Is On? 1 Answer
Changing Color of Curser 2 Answers
How do I define the SetCursor Script? 1 Answer
Make GameObject match the position of mouse cursor? 0 Answers