Mouse control

On desktop, a significant part of our users plays outside fullscreen mode. Your game should avoid users accidentally leaving the game by clicking outside of the game frame. We recognize 3 different game types:

  • First-person games:
    • Lock the mouse in the center of the screen during gameplay
    • Implement a keyboard shortcut to unlock the mouse (e.g. Escape, Tab)
    • Examples: Bloxd.io
  • Top-view games in which the character moves based on mouse gestures (Agar.io, GunMaster.io, …). In these games, we require your game to:
    • Lock the mouse & confine it to the game area
    • Show a custom pointer or use an alternative method to display the mouse position (‘joystick’).
    • Implement a keyboard shortcut to unlock the mouse (e.g. Escape, Tab)
    • If any UI buttons are shown during gameplay, they should either be clickable or a keyboard shortcut should be indicated.
    • Optionally, the game could also add WASD/Arrow keys
    • Examples: Little Big Fighters, GunMaster.io
  • Other games, where most actions are taken with mouse clicks on the UI (clickers, bubble shooter, …)
    • In these games with limited mouse movement, we do not require mouse confinement.

Here is a code example that uses customMouse as a variable that contains a custom mouse image. If the game has UI buttons, please refer to the requirements above:

public Image customMouse;
public RectTransform canvas;

void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

void Update()
{
    // get the mouse movement
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");

    // get the custom mouse position
    Vector2 currentPosition = customMouse.rectTransform.localPosition;

    // update the custom mouse position
    currentPosition.x += mouseX;
    currentPosition.y += mouseY;

    // clamp the mouse position within the canvas
    currentPosition.x = Mathf.Clamp(currentPosition.x, canvas.rect.min.x, canvas.rect.max.x);
    currentPosition.y = Mathf.Clamp(currentPosition.y, canvas.rect.min.y, canvas.rect.max.y);

    // set the new custom mouse position
    customMouse.rectTransform.localPosition = currentPosition;

    // check if the left mouse button click
    if (Input.GetMouseButtonDown(0))
    {
        // do action
    }
}