Correct way to fetch mouse position #7970
-
Since bevy 0.9.0 bevy::window:Window does not implement the Resource trait anymore so this cookbook example does not work anymore. What's the cleanest way to access the mouse position with bevy 0.10.0 and convert it to world space? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
fn my_cursor_system(
windows: Query<&Window>,
camera_q: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
) {
let window = windows.single();
let (camera, camera_transform) = camera_q.single();
if let Some(world_position) = window
.cursor_position()
.and_then(|cursor| camera.viewport_to_world_2d(camera_transform, cursor))
{
eprintln!("World coords: {}/{}", world_position.x, world_position.y);
}
} |
Beta Was this translation helpful? Give feedback.
-
For those (like me) that got stuck here, on bevy 0.15 this is now a Result, and not an Option.
Sorry for the necro bump |
Beta Was this translation helpful? Give feedback.
Window
is now a component you need to query for.I've updated the system to 0.10 for you :)