- Home /
Locking the cursor in Unity 5?
All of the answer thread I've seen have not helped, the Unity 5 cursor locking is confusing ; and the old way of doing it doesn't work anymore.
Can someone please explain how this all works? NOTHING, helped. The documentation, other thread, etc.
Answer by tanoshimi · May 28, 2015 at 10:16 PM
What part of the documentation don't you understand? It seems pretty explicit to me: http://docs.unity3d.com/ScriptReference/Cursor-lockState.html
Answer by erebel55 · May 28, 2015 at 11:04 PM
Cursor.lockState = CursorLockMode.Locked;
and you probably want to hide the cursor too
Cursor.visible = false;
Answer by Whiteleaf · May 29, 2015 at 09:17 AM
I tried yours Erebel, but when I do it locks my cursor in the bottom left of my screen. My monitor resolution is around 1600x1000; I don't know if that's it.
Tano, it just didn't explain it enough for me. There's no "If this happens, then...", and things like that. It literally just told you what the code was and no debugging/issues that might occur.
Please post your comments with the "add new comment" button ins$$anonymous$$d of as an answer.
Screen resolution should have nothing to do with it.
Where are you calling Cursor.lockState?
Try calling it within Update() like so..
public class $$anonymous$$ouseLock : $$anonymous$$onoBehaviour
{
void Start()
{
// hide the cursor
Cursor.visible = false;
}
void Update()
{
if (Cursor.lockState != CursorLock$$anonymous$$ode.Locked)
{
// lock the cursor
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
}
}
}
Oops sorry this is my first answers post, so I didn't know there was a "add comment" button. I'll try your code and hopefully it works. I was putting it in start because later on if say I had an Inventory, wouldn't I not want it to be locked every frame?
Okay so now when I lock the cursor, it locks it at the top of the screen next top the pause button and even when I tell it to be invisible it is visible. Why is this happening?
EDIT: After a $$anonymous$$ute of letting playmode sit it seemed to disappear but I'm not entirely sure if it was in the middle which kinda bothers me, but nonetheless it's locked.
Hope it doesn't effect it in the standalone, thanks for the help people!
I'm not going to; as it didn't help. I thanks because people actually replied, I already know how to do everything you gave me in that code.
But I also found a new problem, with a $$anonymous$$ouseLook script I followed from a Unity 5 tutorial, the script works fine, but when I press any key the mouse flickers even though I told it to be invisible. It also is locking at random locations and doesn't disappear till I click on the screen.
Would it be that I have a 32-bit operating system? I'm currently running Windows XP Professional; as soon as I get Windows 8 maybe that'll fix it. Also would it maybe be because I can only have DirectX 9 on Windows XP?
EDIT: Okay, so as it turns out it does not have this affect in the standalone, but only in the editor. I'm guessing it's a bug and no one else has experienced it maybe? I'm running the newest version of Unity 5, Personal Edition.
Your answer
Follow this Question
Related Questions
Mouse cursor doesn't lock Unity 5 0 Answers
Cursor Lockmode only works once? 1 Answer
Cursor bug 0 Answers
Keep Cursor at center and project interactions to cursor position on mobile touch devices (IOS) 0 Answers
Cursor.lockState does not work 2 Answers