Search Results for

    Show / Hide Table of Contents

    Mouse

    The Mouse is one of the most common input devices on desktop platforms. You can access the mouse state by using the Input class:

    • Input.MousePosition
    • Input.MousePositionDelta
    • Input.MouseScrollDelta
    • Input.GetMouseButton
    • Input.GetMouseButtonDown
    • Input.GetMouseButtonUp

    Locking cursor

    In some games you may want to lock the mouse position or hide the cursor during gameplay. For instance, first-person shooter games usually need 360-degree camera rotation, and you don't want to click off the game or have the cursor blocking gameplay.

    You can lock the mouse movement by using Screen.CursorLock, and modify the cursor visibility by using Screen.CursorVisible.

    Usage

    In your C# script you can read mouse button inputs:

    public override void OnUpdate()
    {
        if (Input.GetMouseButton(MouseButton.Left))
        {
            Debug.Log("Left mouse button is pressed.");
        }
    }
    

    You can read how much the mouse has moved:

    public override void OnUpdate()
    {
        Float2 delta = Input.MousePositionDelta;
        Debug.Log("The mouse movement since last frame is: " + delta);
    }
    

    And you can lock and hide the mouse cursor:

    public override void OnStart()
    {
        Screen.CursorLock = CursorLockMode.Locked;
        Screen.CursorVisible = false;
    }
    
    • Improve this Doc
    In This Article
    Back to top Copyright © 2012-2024 Wojciech Figat