#Listen for mouse click with world position

4 messages · Page 1 of 1 (latest)

sinful storm
#

I'm trying out mouse inputs, so far I am moving my spawners on mouse click 1 tile to the right with this code:

fn move_sources(
    mut spawners: Query<(&mut TilePosition, &MoveOnClick)>,
    mut mouse_input: Res<Input<MouseButton>>,
) {
    for (mut spawner, move_on) in &mut spawners {
        if mouse_input.just_pressed(move_on.mouse_button) {
            spawner.x = (spawner.x + 1) % 10; // TODO: hardwired level width
        }
    }
}

This is working well, but now I want to move the spawner to the position where the mouse click happened. Is there a way to get the click coordinates from the mouse input event? I have a simple 2D game with a tile grid.

I tried looking at this example: https://bevy-cheatbook.github.io/cookbook/cursor2world.html but it looks so damn complicated. I guess there can be multiple worlds, so getting the world coordinates needs you to specify which world's coordinates you want?

Is there a more simple way to get the world coordinates of the click when it happens?

sinful storm
#

Ok, I decided to give the tutorial a try, but I cannot find the camera. I initialize the camera like this:

// in setup:
app.add_startup_system(setup_camera);
app.add_system_to_stage(CoreStage::First, move_sources);

fn setup_camera(mut commands: Commands) {
    let mut camera = Camera2dBundle::default();

    let scale = 0.02f32;
    let position = 5f32;

    //camera.transform = Transform::from_scale(Vec3::new(scale, scale, scale));
    camera.transform.scale.x = scale;
    camera.transform.scale.y = scale;

    //camera.transform.translation = Vec3::new(position, position, 0f32);
    camera.transform.translation.x = position;
    camera.transform.translation.y = position;

    commands.spawn(camera);
}

fn move_sources(
    mut spawners: Query<(&mut TilePosition, &MoveOnClick)>,
    mut mouse_input: Res<Input<MouseButton>>,
    windows: Res<Windows>,
    //camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>, // TODO: handle multiple cameras?
    camera: Query<(&Camera, &GlobalTransform)>,
    camera1: Query<&Camera>,
    camera2: Query<&GlobalTransform>,
) {
    println!("Camera: {:?}", camera1.get_single());
    println!("Global transform: {:?}", camera2.get_single());

    // get the camera info and transform
    // assuming there is exactly one main camera entity, so query::single() is OK
    let (camera, camera_transform) = camera.get_single().expect("Camera should exist");

    // ...
}
#

Output:

Output:

Camera: Err(NoEntities("bevy_ecs::query::state::QueryState<&bevy_render::camera::camera::Camera>"))
Global transform: Err(NoEntities("bevy_ecs::query::state::QueryState<&bevy_transform::components::global_transform::GlobalTransform>"))
thread 'Compute Task Pool (4)' panicked at 'Camera should exist: NoEntities("bevy_ecs::query::state::QueryState<(&bevy_render::camera::camera::Camera, &bevy_transform::components::global_transform::GlobalTransform)>")', src\systems\input.rs:43:58
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', C:\Programs\Cargo\bin\registry\src\github.com-1ecc6299db9ec823\bevy_tasks-0.9.1\src\task_pool.rs:273:45
error: process didn't exit successfully: `target\debug\liqui-slime-bevy.exe` (exit code: 101)
#

Neither Camera or GlobalTransform are found, even though they are a part of the Camera2dBundle that I am adding. What am I doing wrong?