First-person and third-person games need a hidden, locked mouse cursor so the player can look around without the pointer drifting off the screen. Menus need the opposite: a visible, free cursor. Getting between the two is where most bugs live. This guide explains how to lock and hide the mouse cursor in Unity, how to get it back for pause menus and inventories, why it behaves differently in the Editor and in WebGL, and how to set a custom cursor image.
Lock and Hide: the Basics
Two static properties on the Cursor class do the work:
using UnityEngine;
public class CursorControl : MonoBehaviour
{
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
Cursor.lockState controls where the cursor can go. Cursor.visible controls whether it is drawn. They are separate. Setting lockState to Locked also hides the cursor in practice, but setting visible explicitly keeps your intent clear and avoids surprises when switching modes. Mouse movement still reports as deltas while the cursor is locked, so camera look code keeps working. If you are building a camera controller, see the first person controller tutorial.
The Three Lock Modes
| CursorLockMode | Behavior | Use for |
|---|---|---|
| None | Cursor moves freely and can leave the window | Menus, inventory, pause screens |
| Locked | Cursor is fixed at the center of the screen | First and third person cameras |
| Confined | Cursor can move but stays inside the game window | Strategy and top-down games with edge scrolling |
Confined is not supported on every platform, so test on your targets. While the cursor is locked, the pointer position stays at the center, so do not use it for aiming UI clicks. Use the mouse delta for looking, and a center-screen ray for aiming.
Switching for Menus and Pausing
The clean way is one small helper that your menus call. That keeps the cursor state in one place instead of scattered across scripts.
using UnityEngine;
public static class CursorState
{
public static void Gameplay()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
public static void UI()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
Then in a pause menu:
public void SetPaused(bool paused)
{
Time.timeScale = paused ? 0f : 1f;
pausePanel.SetActive(paused);
if (paused) CursorState.UI();
else CursorState.Gameplay();
}
The most common bug is opening a menu without calling the UI version. The panel appears, but the cursor is still locked and hidden, so the player cannot click anything. Every menu, inventory, dialogue box and death screen needs to switch the cursor on open and switch back on close. If several panels can be open at once, keep a counter of open panels and only return to Gameplay when it reaches zero. The pause menu guide shows the pause side of this, and the main menu tutorial covers menu scenes, which should start with an unlocked cursor.
When the cursor is free over a HUD, remember clicks can pass through UI into the game world. The UI click-through guide explains how to block that.
Editor Behavior and the Escape Key
In the Editor, pressing Escape releases a locked cursor so you can get back to the Unity windows. This is built in, and Unity then treats the cursor as unlocked until the Game view is clicked again. Several things follow from that:
- If your game uses Escape to open the pause menu, the Editor frees the cursor at the same time. The menu works, but after closing it you may need to click in the Game view before the lock returns.
- Setting
lockState = Lockedonly takes effect when the Game view has focus. If you press Play with another window focused, the lock may not apply until you click the Game view. - Test pause menus in a real build as well. The Editor’s Escape handling does not exist there, so the behavior can differ.
To avoid fighting the Editor, use a different key for pause while developing, or accept the extra click. Some teams map pause to both Escape and P.
WebGL Pointer Lock
In a browser, Unity uses the browser’s Pointer Lock API, which has strict rules:
- A user gesture is required. Calling
Cursor.lockState = LockedinStartdoes nothing on WebGL, because there was no click. Lock the cursor in response to a click, such as a Play button or a click on the game canvas. - Escape always exits pointer lock, and the game cannot stop that. The browser owns that key.
- Re-locking right after Escape can fail. Browsers often reject a lock request made immediately after the user exited. Make the player click again, and show a “Click to resume” message rather than relocking automatically.
A pattern that works well on WebGL: in your update, if the game is in gameplay mode but Cursor.lockState is not Locked, show a “Click to play” overlay. When the player clicks it, lock the cursor and hide the overlay. This also handles the Escape case.
void Update()
{
bool shouldBeLocked = inGameplay;
bool isLocked = Cursor.lockState == CursorLockMode.Locked;
clickToResumePanel.SetActive(shouldBeLocked && !isLocked);
if (shouldBeLocked && !isLocked && Mouse.current.leftButton.wasPressedThisFrame)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
This uses UnityEngine.InputSystem for the click. For general WebGL concerns like build size and load time, see the WebGL optimization guide.
Custom Cursors
To replace the arrow with your own image, use Cursor.SetCursor:
[SerializeField] Texture2D cursorTexture;
[SerializeField] Vector2 hotspot = Vector2.zero;
void Start()
{
Cursor.SetCursor(cursorTexture, hotspot, CursorMode.Auto);
}
// to restore the system cursor
// Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
The steps:
- Import the image and set its Texture Type to Cursor in the Inspector (or use Default with read/write enabled; Cursor is the proper option).
- Keep it small. 32×32 is typical. Large cursors can be rejected by the operating system in hardware mode.
- Set the hotspot to the pixel that should act as the click point, measured from the top left of the image. A crosshair has its hotspot at the center, an arrow at the tip.
- Use
CursorMode.Autofor the hardware cursor. UseCursorMode.ForceSoftwareif the hardware cursor looks wrong or the image is larger than the platform supports.
You can swap the texture at any time: a hand over interactive objects, a crosshair when aiming. For a fully custom look with animation, hide the system cursor and move a UI Image to the mouse position yourself, which works in any mode and on any resolution. For a locked camera, a crosshair is just a UI Image at the center of the Canvas; no cursor involvement needed.
Losing and Regaining Focus
When the player alt-tabs away, the cursor is released automatically. When they come back, the lock may not be restored. Handle it explicitly:
void OnApplicationFocus(bool hasFocus)
{
if (hasFocus && inGameplay)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
Use a real inGameplay flag from your game state so you never lock the cursor while a menu is open. On mobile, the cursor APIs do nothing, so wrap them in #if checks or a platform test if your code shares a project with phone builds.
📚 Learn UI and input design
Cursor handling is part of good UX. Our list of programming and game design books has picks that cover game interface design.
Troubleshooting
Cursor is visible but cannot click menu buttons
The cursor is not locked, but a UI problem blocks clicks: a missing EventSystem, a missing Graphic Raycaster, or a transparent image covering the buttons. Check the Event System first; see the UI button not working guide.
Cursor stays hidden after opening a menu
You never set Cursor.visible = true and lockState = None when the menu opened. Use a central helper.
Cursor does not lock when I press Play
The Game view does not have focus. Click inside it. This is an Editor behavior, not a bug in your script.
Cursor lock works in the Editor but not in the WebGL build
WebGL needs a user click before it can lock. Add a “Click to play” step. Also remember Escape always releases the lock in browsers.
Cursor appears again after pressing Escape
This is expected in the Editor and in browsers. Treat Escape as “open pause menu” and call your UI helper at the same time.
Custom cursor is blurry or the wrong size
The texture is compressed or scaled. Set the Texture Type to Cursor, turn off compression and use a small size like 32×32. Try CursorMode.ForceSoftware if the OS rescales it.
Mouse look stops when the cursor hits the screen edge
The cursor is not locked. In Locked mode the delta continues forever. In None mode the pointer stops at the screen edge and so does the delta.
FAQ
Do I need both Cursor.visible and Cursor.lockState?
Set both. Locked usually hides the pointer by itself, but being explicit prevents bugs when you switch to Confined or None.
Does Confined mode work on all platforms?
Not everywhere. It works on desktop builds, but behavior varies by platform and in the browser, so test it on each target.
How do I get the mouse position while the cursor is locked?
You do not. The position stays at the center. Use mouse delta for looking and raycast from the screen center for aiming.
Can I have a gamepad and mouse in the same game?
Yes. Hide the cursor when the last input came from a gamepad and show it again when the mouse moves. Your input system can report which device was used most recently.
Is there a way to stop Escape unlocking the cursor in the Editor?
Not reliably. Pick another pause key while developing, or accept the extra click in the Game view.
Reader questions
Asked by readers of this article and answered by AI. Behavior can differ between Unity versions, so test in yours.




Leave a Reply