-
Hi, In games where the cursor is hidden (e.g. FPS), it's common to look at objects in order to pick/interact with them. Based on this issue (bevyengine/bevy#2645) I don't think it's possible to lock the cursor to the centre of the screen currently, so is there a way to use the viewport centre instead of the cursor for picking objects? For example, in the video below I'd want to pick the cube anytime I hover over it with the white square in the centre of the screen. demo.mp4 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Basically, instead of constantly moving the camera ray to match the mouse, all you have to do is set it to the center of the screen and leave it alone. I'd recommend using bevy_mod_raycast so you can get the items under the reticle and the mouse seperately, but if you want something quick and dirty that works, I think the easiest way to do this is to simply overwrite the value that the plugin adds from the mouse: // When you add this system in your app make sure it is set to run
// after `PickingSystem::UpdatePickSourcePositions`
// AND before `PickingSystem::BuildRays`
fn reset_pointer(
mut pick_source_query: Query<&mut PickingCamera>,
) {
pick_source.cast_method = RayCastMethod::Transform;
} |
Beta Was this translation helpful? Give feedback.
-
Hi - thanks for your reply! I tried the quick method you mentioned, but it didn't produce the desired result by itself. I ended up doing 2 things:
let mut picking_camera_bundle = PickingCameraBundle::default();
picking_camera_bundle.source.cast_method = RayCastMethod::Transform;
Code below: use bevy::prelude::*;
use bevy::app::PluginGroupBuilder;
use bevy::ecs::schedule::ShouldRun;
use bevy_mod_picking::{DebugCursorPickingPlugin, HighlightablePickingPlugins, InteractablePickingPlugin, PickingPluginsState, PickingRaycastSet, PickingSystem};
/// A custom version of DefaultPickingPlugins.
pub(super) struct CustomPickingPlugins;
impl PluginGroup for CustomPickingPlugins {
fn build(&mut self, group: &mut PluginGroupBuilder) {
group.add(CustomPickingPlugin);
group.add(InteractablePickingPlugin);
group.add(DebugCursorPickingPlugin);
HighlightablePickingPlugins.build(group);
}
}
/// A custom version of PickingPlugin which doesn't change the raycast direction.
struct CustomPickingPlugin;
impl Plugin for CustomPickingPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<PickingPluginsState>()
.add_system_set_to_stage(
CoreStage::First,
SystemSet::new()
.with_run_criteria(|state: Res<PickingPluginsState>| {
simple_criteria(state.enable_picking)
})
// The update_pick_source_positions system is excluded here
.with_system(
bevy_mod_raycast::build_rays::<PickingRaycastSet>
.label(PickingSystem::BuildRays)
.before(PickingSystem::UpdateRaycast),
)
.with_system(
bevy_mod_raycast::update_raycast::<PickingRaycastSet>
.label(PickingSystem::UpdateRaycast)
.before(PickingSystem::UpdateIntersections),
)
.with_system(
bevy_mod_raycast::update_intersections::<PickingRaycastSet>
.label(PickingSystem::UpdateIntersections),
),
);
}
}
fn simple_criteria(flag: bool) -> ShouldRun {
if flag {
ShouldRun::Yes
} else {
ShouldRun::No
}
} Just out of interest, I noticed that |
Beta Was this translation helpful? Give feedback.
Hi - thanks for your reply!
I tried the quick method you mentioned, but it didn't produce the desired result by itself.
I ended up doing 2 things:
PickingCameraBundle
to useRayCastMethod::Transform
as the cast_method before adding it to my Camera entity - e.gPickingPlugin
which excludes theupdate_pick_source_positions
system.Code below: