Grabbing the Mouse
Click here for the full example code.
Relevant official examples:
mouse_grab
.
You can lock/release the mouse cursor using bevy's window settings API.
Here is an example that locks and hides the cursor in the primary window
on mouse click and releases it when pressing
Esc
:
fn cursor_grab_system(
mut windows: ResMut<Windows>,
btn: Res<Input<MouseButton>>,
key: Res<Input<KeyCode>>,
) {
let window = windows.get_primary_mut().unwrap();
if btn.just_pressed(MouseButton::Left) {
window.set_cursor_lock_mode(true);
window.set_cursor_visibility(false);
}
if key.just_pressed(KeyCode::Escape) {
window.set_cursor_lock_mode(false);
window.set_cursor_visibility(true);
}
}