Skip to content

Commit 5566d73

Browse files
committed
Nicer usage for scene viewer (#7035)
# Objective Scene viewer mouse sensitivity/cursor usage isn't the best it could be atm, so just adding some quick, maybe opinionated, tweaks to make it feel more at home in usage. ## Solution - Mouse delta shouldn't be affected by delta time, it should be more expected that if I move my mouse 1 inch to the right that it should move the in game camera/whatever is controlled the same regardless of FPS. - Uses a magic number of 180.0 for a nice default sensitivity, modeled after Valorant's default sensitivity. - Cursor now gets locked/hidden when rotating the camera to give it more of the effect that you are grabbing the camera.
1 parent 741a91e commit 5566d73

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

examples/tools/scene_viewer/camera_controller_plugin.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
//! - Copy the code for the `CameraControllerPlugin` and add the plugin to your App.
44
//! - Attach the `CameraController` component to an entity with a `Camera3dBundle`.
55
6+
use bevy::window::CursorGrabMode;
67
use bevy::{input::mouse::MouseMotion, prelude::*};
78

89
use std::f32::consts::*;
910
use std::fmt;
1011

12+
/// Based on Valorant's default sensitivity, not entirely sure why it is exactly 1.0 / 180.0,
13+
/// but I'm guessing it is a misunderstanding between degrees/radians and then sticking with
14+
/// it because it felt nice.
15+
pub const RADIANS_PER_DOT: f32 = 1.0 / 180.0;
16+
1117
#[derive(Component)]
1218
pub struct CameraController {
1319
pub enabled: bool,
@@ -35,7 +41,7 @@ impl Default for CameraController {
3541
Self {
3642
enabled: true,
3743
initialized: false,
38-
sensitivity: 0.5,
44+
sensitivity: 1.0,
3945
key_forward: KeyCode::W,
4046
key_back: KeyCode::S,
4147
key_left: KeyCode::A,
@@ -91,12 +97,14 @@ impl Plugin for CameraControllerPlugin {
9197

9298
fn camera_controller(
9399
time: Res<Time>,
100+
mut windows: ResMut<Windows>,
94101
mut mouse_events: EventReader<MouseMotion>,
95102
mouse_button_input: Res<Input<MouseButton>>,
96103
key_input: Res<Input<KeyCode>>,
97104
mut move_toggled: Local<bool>,
98105
mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
99106
) {
107+
let window = windows.primary_mut();
100108
let dt = time.delta_seconds();
101109

102110
if let Ok((mut transform, mut options)) = query.get_single_mut() {
@@ -158,16 +166,22 @@ fn camera_controller(
158166
// Handle mouse input
159167
let mut mouse_delta = Vec2::ZERO;
160168
if mouse_button_input.pressed(options.mouse_key_enable_mouse) || *move_toggled {
169+
window.set_cursor_grab_mode(CursorGrabMode::Locked);
170+
window.set_cursor_visibility(false);
171+
161172
for mouse_event in mouse_events.iter() {
162173
mouse_delta += mouse_event.delta;
163174
}
175+
} else {
176+
window.set_cursor_grab_mode(CursorGrabMode::None);
177+
window.set_cursor_visibility(true);
164178
}
165179

166180
if mouse_delta != Vec2::ZERO {
167181
// Apply look update
168-
options.pitch = (options.pitch - mouse_delta.y * 0.5 * options.sensitivity * dt)
182+
options.pitch = (options.pitch - mouse_delta.y * RADIANS_PER_DOT * options.sensitivity)
169183
.clamp(-PI / 2., PI / 2.);
170-
options.yaw -= mouse_delta.x * options.sensitivity * dt;
184+
options.yaw -= mouse_delta.x * RADIANS_PER_DOT * options.sensitivity;
171185
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, options.yaw, options.pitch);
172186
}
173187
}

0 commit comments

Comments
 (0)