Hey guys, as the title suggests I wish to know how to lock the cursor position to the center of the screen. Screen.lockCursor doesnt seem to be what I'm after as this hides the cursor (good) and restricts it from leaving the game screen, but i need something that also LOCKS the mouse in the center of the screen. Even if there was a way for me to reset the mouse cursor every frame?? Basically I'm doing a 3rd person shooter with a GUI crosshair, but I want to use onMouseDown on my enemies. So I need my cursor to sit where my crosshair is to make aiming accurate. Thanks in advanced. Jace
actually it seems lockCursor DOES lock the mouse. However for somereason it isn't locking it in the middle of the screen, its locking the cursor at some arbitrary position above and to the left of the center. Any ideas about why it could be doing that??? Jace
grr now it IS locking in the center of the screen sometimes and other times it is off at that same random position (Up and to the left of the center of the screen)
search the forum " Custom cursor or check http://www.unifycommunity.com/wiki/index.php?title=Custom_2D_Pointer should point you to what you need and more :wink: or lock the cursor and use a game object with a custom texture of cross hair and attach to the camera as a child, something like that.
Pardon the necromancy =) For prototyping I use this: function Update () { Screen.lockCursor = true; Screen.lockCursor = false; } Screen.lockCursor moves the pointer to the center of the screen, so it does exactly what the OP suggested - resets the cursor every frame. You could also use a MouseMove event for example Not very clean but it does the job, you retain OnMouseDown functionality, the cursor remains visible.
if your using C# i use this for setting the value to true or false if paused public static bool paused = false; void Start() { Time.timeScale = 1; } void Update () { Screen.lockCursor = true; if(paused ==false) Screen.lockCursor = false; if(Input.GetKeyDown(KeyCode.Escape) paused == false) { paused = true; Time.timeScale = 0; } else if(Input.GetKeyDown(KeyCode.Escape) paused == true) { paused = false; Time.timeScale = 1; } }
If you in any case want to lock the cursor in any other position than the center you can do it by using an API i created and released to the asset store. It is as easy as this "SetCursorPosition(x : int,y : int)" calling this every frame will prevent the cursor from changing coordinates http://forum.unity3d.com/threads/242832-Official-Set-Cursor-Position?p=1606714#post1606714