#Multiple On<Pointer<Drag>> on same entity

6 messages · Page 1 of 1 (latest)

unreal remnant
#

So basically i wanna move an object and also set its gravity to 0, and then set it to normal gravity when its dropped

My current code (which panics) ```
On::<Pointer<Drag>>::target_component_mut::<Transform>(|drag, transform| {
transform.translation.x += drag.delta.x;
transform.translation.y -= drag.delta.y;
}),
On::<Pointer<Drag>>::target_commands_mut(|_event, commands| {
commands.insert(GravityScale(0.0));
}),
On::<Pointer<Drop>>::target_commands_mut(|_event, commands| {
commands.insert(GravityScale(1.0));
}),

I just wanna find a way to better do this ^^^
unreal remnant
#

Moving objects with bevy_mod_picking

#

Multiple On<Pointer<Drag>> on same entity

main wharf
#

From a quick look at the source code of On::target_component_mut for example (you can see its documentation at https://docs.rs/bevy_eventlistener/latest/bevy_eventlistener/event_listener/struct.On.html#method.target_component_mut and then click on the source link on the right) you can see it internally just calls On::run, which is a public method) that lets you run any system (so you can take both a Commands and a Query<&mut Transform>). What's missing is the entity id on which you clicked, which in target_component_mut seems to be retrieved by calling .target() on a Res<ListenerInput<E>>.

#

So you should end up with something like this for the first two On::<Pointer<Drag>> (the third one is different so it can remain like that)

On::<Pointer<Drag>>::run(|
    mut commands: Commands,
    event: Res<ListenerInput<Pointer<Drag>>>,
    mut transform_query: Query<&mut Transform>,
| {
    let entity = event.target();
    let mut transform = transform_query.get(entity).unwrap();
    transform.translation.x += drag.delta.x;
    transform.translation.y -= drag.delta.y;
    commands.entity(entity).insert(GravityScale(0.0));
})
#

Though now that I look a bit more at what you're trying to do, you could use the DragEnter event for running the commands.insert(GravityScale(0.0));, in which case the three components would all have different type and there should be no problem