#Sprite Picking does not work after migrating from 0.15.3 to 0.16.1

44 messages · Page 1 of 1 (latest)

sharp hearth
#

I have a sprite that can be picked:

commands.spawn((
  Sprite::from_image(asset_server.load("Player1.png")),
  Pickable::default(),
  Transform::from_xyz(-490.0, -440.0, 1.0).with_scale(Vec3::new(2.2, 2.2, 1.0)),
))
.observe(on_dragged)
.insert(TowerDroppable {
  dragging: false,
});

the first thing on_dragged does is println!("dragged");
this never gets run when dragging the sprite
I have a 3D and 2D camera, I have added the component "SpritePickingCamera" to both, when adding SpritePickingPlugin to the app I get an error saying it is already added, which is strange as in the documentation it says it is no longer in default plugins. However in the sprite picking example on the bevy website there is no SpritePickingPlugin added and the camera doesn't even have SpritePickingCamera, however the example works, and my code doesn't so I am confused on why this is occurring. If I add SpritePickingPlugin to the example I also get the same error: "Error adding plugin bevy_sprite::picking_backend::SpritePickingPlugin: : plugin was already added in application", so I am assuming the plugin is in DefaultPlugins even though the docs say it is not.

sharp hearth
#

so I copy pasted the sprites from the example and their fucntionality into my game and it seems the function specified in the observe runs once or a few times for each sprite and then does not run again when being clicked or released

empty cypress
sharp hearth
#

it does work in 0.16.1

sharp hearth
#

as both the example and my own sprite do nothing when interacted with

#
pub fn setup_game_camera(mut commands: Commands) {
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(0.0, 10.0, 0.0).looking_at(Vec3::new(0.0, 0.0, -6.0), Vec3::Y),
        SpritePickingCamera,
        Camera {
            order: 0,
            target: RenderTarget::Window(WindowRef::Primary),
            ..default()
        },
        GameCamera,
    ));
}
pub fn setup_ui_camera(
    mut commands: Commands
) {
    let camera2d = (
        Camera2d,
        Camera {
            order: 1,
            target: RenderTarget::Window(WindowRef::Primary),
            ..default()
        },
        Transform::from_xyz(0.0, 0.0, 0.0),
        GlobalTransform::default(),
        SpritePickingCamera,
        Projection::custom(
            OrthographicProjection{
                scaling_mode: ScalingMode::Fixed { width: 1920.0 , height: 1080.0 }, 
                ..OrthographicProjection::default_2d()
            }
        )
    );

    commands.spawn(camera2d);
}

this is the code for both my cameras

#

this all worked in 0.15.3

#

I migrated to 0.16.1, fixed all errors, but now my picking just doesn't work on sprites, it works on meshes though with the MeshPickingPlugin

#

and the mesh has the same code basically

#
                commands.spawn((
                    Mesh3d(floor_tile_mesh.clone()),
                    MeshMaterial3d(material_handle.clone()),
                    Transform::from_xyz(position.x as f32, position.y as f32, position.z as f32),
                ))
                .observe(on_map_drag);

mesh code, I use .observe() on both, and the mesh doesn't even have any pickable components or anything

vernal plover
#

Make sure you have the necessary features on your cargo.toml

sharp hearth
#

the cargo.toml for both is the same, and they have "bevy_picking"

#

I copied it over to a new rust project and copied the example code into main.rs

vernal plover
sharp hearth
#

the sprites are missing but I can tell the picking works as the black boxes change color when hovered and clicked

vernal plover
#

bevy_sprite_picking_backend

sharp hearth
#

also the example works without that (because it is default)

#

I changed the example to be more simple (like in my code) and it works there

#

I do

                    .observe(ev);

on the spawn command then

fn ev(_ev: Trigger<Pointer<Drag>>){
    println!("A");
}
#

and it works

#

it prints A when dragged

#

but in my game this doesn't work

#

I just commented out my 3D camera (also had to comment out alot of other code because of it) again, and it still doesn't work, even with the 2D camera only

#

I got it to work by stripping my game down to just 2 plugins I made, UI camera and drag and drop system

#

gonna start enabling stuff to see what breaks it

#
        Projection::custom(
            OrthographicProjection{
                scaling_mode: ScalingMode::Fixed { width: 1920.0 , height: 1080.0 }, 
                ..OrthographicProjection::default_2d()
            }
        )
#

this breaks it

#

any idea why

vernal plover
#

Why custom?

sharp hearth
#

I used to just have OrthographicProjection and I saw in migration docs to use Projection::custom instead now

#

in 0.16.1

#

used to work with picking in 0.15.3

#

I believe OrthographicProjection might have been required in the past (there were two types of projection and you needed to specify one)

#

not sure

#

I don't know if it changes much, looks the same without it

vernal plover
sharp hearth
#

oh there is still a Orthographic

#

I just read this

#

and tried custom with the orthographic I had before inside

#

and I got no errors

#

so I assumed it was the correct way, either way I can just omit all of it now