Mouse

Reference for the global mouse object, which tracks cursor position, button state, collisions and input routing.

Mouse

mouse is the singleton instance provided by Ursina when you import it. It extends input handling to track cursor movement, clicks, collisions and hover state.

Overview

The mouse object centralizes all pointer information:

  • Absolute and delta movement
  • Button down/up states
  • Drag distances
  • Hovered entity detection via raycasts
  • Double‑click timing and position

You can lock or hide the cursor, adjust collision targets, and read/write all properties at runtime.

Fields

NameTypeDescription
.enabledboolMaster switch for mouse updates.
.visibleboolShow or hide the system cursor.
.lockedboolConfine cursor to window center (for FPS controls).
.positionVec3Current cursor position in world or UI space.
.deltaVec3Movement since last button‑press.
.delta_dragVec3Movement between left‑button down and up.
.velocityVec3Movement per frame.
.movingboolTrue when the cursor is in motion.
.prev_click_timefloatTimestamp of last click.
.prev_click_posVec3Cursor position at last click.
.double_click_distancefloatMax move distance to still count as double‑click.
.double_click_movement_limitfloatThreshold movement to interrupt double‑click detection.
.hovered_entityEntityClosest entity under cursor with a collider.
.left, .right, .middleboolButton down states.
.traverse_targetEntityScene or parent under which to raycast for collisions.
.raycastboolEnable or disable pointer raycasts.
.collisionslistList of hits from the last raycast.

Properties

PropertyDescription
.x, .yShortcut to cursor coordinates.
.lockedGet/set lock state.
.visibleGet/set cursor visibility.
.normalSurface normal at hit point (local space).
.world_normalSurface normal at hit point (world space).
.pointHit position on collider (local space).
.world_pointHit position on collider (world space).
.is_outsideTrue if cursor is outside window or no collision detected.

Methods

MethodDescription
input(key)Internal handler for mouse button events.
update()Update cursor position, delta, velocity and collisions each frame.
find_collision()Perform raycast against .traverse_target to update .collisions.
unhover_everything_not_hit()Clear hover state on entities not under the cursor.

Example Usage

from ursina import *
from ursina import Ursina, Button, mouse

app = Ursina()

# create a button in the scene
Button(parent=scene, text='Click me')

def input(key):
    if key == 'space':
        # toggle cursor lock and print velocity
        mouse.locked = not mouse.locked
        print('cursor velocity:', mouse.velocity)

# show a custom cursor
Cursor()

app.run()