- Home /
Locking the Cursor at the center.
I've been looking at answers, and I don't understand them. All I want is a script that will lock the cursor at the center of the screen for a stand-alone game. Just one script, NOTHING ELSE.
Except for the simplest of things, it rarely works to simply drop a script into a project and have it do what you want, and your request is non-trivial. You're probably best off studying people's proposed answers to see what might work best in your situation, and perhaps finding a programmer friend you can talk to face to face to explain some of the more intricate details.
i would say (from my expert knowledge. J$$anonymous$$.) that you would use this (C#)
Screen.lockCursor = false;
to unlock the cursor from the center. to lock it :
Screen.lockCursor = true;
`
and to show the cursor on the screen :
Cursor.visible = true;
and to make it unvisible :
Cursor.visible = false;
hope this helps!
Answer by juan_txo · Jan 13, 2011 at 10:53 AM
Hello.
I needed what you want during a testing proyect. I used "Screen.lockCursor" to block the cursor in the center of screen to simulate the pointer of a gun.
Here you have the API example: ScreenLookCursor
I hope its what you want. See you.
Check the documentation, this will tell you : http://docs.unity3d.com/ScriptReference/Screen-lockCursor.html
Obsolete : Property lockCursor has been deprecated. Use Cursor.lockState and Cursor.visible ins$$anonymous$$d.
Always check the Unity Scripting Reference !
Cursor.lockState : http://docs.unity3d.com/ScriptReference/Cursor-lockState.html
Cursor : http://docs.unity3d.com/ScriptReference/Cursor.html
@andrewjw : Screen.lockCursor and Cursor.lockState are persistent, once it is set you should never have to set them for every Update nor OnGUI.
The only time you would see the cursor flickering is when running in the editor. Screen.lockCursor never worked properly in the editor. This is why you should build your project to see what result the end user will see.
link to example of a tutorial using a toggle cursor command in Update?
Sorry, I'm wrong... they just don't work right in the editor.
Answer by Nsmurf · Jul 18, 2012 at 06:33 PM
void Update()
{
if (Input.GetKey(KeyCode.Escape))
Screen.lockCursor = false;
else
Screen.lockCursor = true;
}
Press and hold escape to unlock the mouse.
It does just add it to your mouse look in the update section I posted the code on this fourm!
Answer by Ravenmore · May 12, 2012 at 10:37 AM
I've been looking at threads about this problem and haven't found an easy enough answer, so I thought I'd add my two cents:
It's not a perfect solution bot for prototyping purposes 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 almost 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.
Screen.lockCursor is depreciated you must now use Cursor.lockState and Cursor.visible ins$$anonymous$$d.
This means you will have to change code like Screen.lockCursor = true; and Screen.lockCursor = false; to Cursor.lockState = CursorLock$$anonymous$$ode.Locked; and Cursor.lockState = CursorLock$$anonymous$$ode.None;
Answer by SectraGaming · Oct 22, 2017 at 02:44 AM
Every thing below this line!
public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;
private float rotY = 0.0f; // rotation around the up/y axis
private float rotX = 0.0f; // rotation around the right/x axis
void Start ()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
}
void Update ()
{
if (Input.GetKey(KeyCode.Escape))
Screen.lockCursor = false;
else
Screen.lockCursor = true;
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
Your answer
Follow this Question
Related Questions
Screen.lockCursor messes my rotation 1 Answer
Locking cursor/mouse on an object 1 Answer
Hide and lock the mouse cursor (beginner) 0 Answers
How to lock mouse? 1 Answer
Locking the mouse cursor without it centering after unlocking it? 0 Answers