- Home /
How can i lock the mouse cursor in the middle of the screen ?
What i'm doing now is in the Update:
void Update ()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.None;
}
But that make the cursor shaking/stuttering when moving the mouse around. And in unity 5.6.1f1 Personal the class Screen don't have the property lockCursor.
Is there any other easy way to center and lock the mouse cursor ?
Why do you lock the cursor and then release it? Remove the 2nd line, and it should work fine.
Just Locked is not working . In Update or Start in both cases the Lock make the mouse cursor to be gone you don't see it at all.
Then, keep the cursor locked and draw a "fake" cursor at the center of the screen.
Found how to do it. In the Start:
void Start ()
{
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.lockState = CursorLock$$anonymous$$ode.Confined;
}
Only Locked will not working. Locked will make the cursor invisible. This way the cursor is visible and locked in center.
Answer by IgorAherne · Sep 23, 2017 at 09:29 AM
Well, you are doing everything correct. It will shake stutter, but it should matter
Don't forget that you can also confine (hide) the cursor. Example here They are using Cursor.lockState = CursorLockState.Confined;
Confine is a bit better but the reason i need the mouse cursor to be locked in the center is that i need it for my adventure game for example to point on a object and then change the mouse cursor for action or take or just the cursor it self. So i have the 3 sprites i need. But they should not be stuttering/shaking when moving around.
In that case, you should be generating (Raycasting) a ray from the camera's viewort's center. This is possible with myCamera.ViewportPointToRay(0.5, 0.5, 0);
and then you can use the resulting ray for intersections, to point at objects, etc. This way we never depend on the cursor's position. It's a little more complicated though
Your answer
Follow this Question
Related Questions
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers