Crate bevy_mouse_tracking_plugin[][src]

Expand description

Tracking the mouse in bevy is kind of annoying. You gotta use Events, and EventReaders, and even then, they only get called when the mouse actually moves.

This crate aims to make this as easy as possible, by providing a static resource that tracks the mouse position every frame.
First, add the plugin to your app:

use bevy::prelude::*;
use bevy_mouse_tracking_plugin::MousePosPlugin;
fn main() {

Now, you can access the resource in your Systems:

use bevy_mouse_tracking_plugin::MousePos;
fn dbg_mouse(mouse: Res<MousePos>) {
    eprintln!("{}", *mouse);

…and don’t forget to add the system to your app:


This will print the screen-space location of the mouse on every frame.

However, we can do better than just screen-space: we support automatic transformation to world-space coordinates.
Change the plugin to this:

fn main() {

In a system…

use bevy_mouse_tracking_plugin::MousePosWorld;
fn dbg_world(mouse: Res<MousePosWorld>) {
    eprintln!("{}", *mouse);
    // Note: the screen-space position is still accessible

This will print the world-space location of the mouse on every frame.
Note that this is only supported for two-dimensional, orthographic camera, but pull requests for 3D support are welcome!

Additionally, we also support a resource that tracks mouse motion, via MouseMotionPlugin. The motion can be accessed from any system in a MouseMotion Res.

As a final aside: the name of this crate is intentionally verbose. This is because I don’t want to steal a crate name, especially since it is very likely that this crate will eventually be made redundant by future updates to bevy.
I recommend renaming the crate in your Cargo.toml:

mouse_tracking = { package = "bevy_mouse_tracking_plugin", version = "..." }


A mouse motion event

Plugin that tracks mouse motion.

The location of the mouse in screenspace.

The location of the mouse in worldspace.


Plugin that tracks the mouse location.

Labels for the various mouse tracking systems, to be used for explicit ordering within stages. The use of this is usually not necessary, as the mouse tracking systems reside in [bevy::app::CoreStage::First].