Skip to content

Correct way to fetch mouse position #7970

Answered by tim-blackbird
liripoop asked this question in Q&A
Discussion options

You must be logged in to vote

Window is now a component you need to query for.
I've updated the system to 0.10 for you :)

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);
    }
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@liripoop
Comment options

Answer selected by liripoop
Comment options

You must be logged in to vote
1 reply
@tim-blackbird
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants