Use the scroll wheel to control the camera speed in examples (#11921)
# Objective - Closes #9384. ## Solution - Make the movement speed of the `CameraController` adjustable with the scroll wheel as mentioned [here](https://github.com/bevyengine/bevy/issues/9384#issuecomment-1668957931). The speed use an exponential progression (10% increase per scroll tick by default) to allow adapting the speed to different scales. - For the `scene_viewer` example, make the default speed proportional to the size of the scene using what's computed for the default camera placement. This gives a good enough default to fly over the scene from the outside. I don't think there's a good way to get a default speed fitting for all scenes since some are meant to be viewed from outside while other are traversable environments.
This commit is contained in:
parent
91b083bb05
commit
8de15ae71a
@ -3,7 +3,7 @@
|
||||
//! - Copy the code for the [`CameraControllerPlugin`] and add the plugin to your App.
|
||||
//! - Attach the [`CameraController`] component to an entity with a [`Camera3dBundle`].
|
||||
|
||||
use bevy::input::mouse::MouseMotion;
|
||||
use bevy::input::mouse::{MouseMotion, MouseScrollUnit, MouseWheel};
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::CursorGrabMode;
|
||||
use std::{f32::consts::*, fmt};
|
||||
@ -37,6 +37,7 @@ pub struct CameraController {
|
||||
pub keyboard_key_toggle_cursor_grab: KeyCode,
|
||||
pub walk_speed: f32,
|
||||
pub run_speed: f32,
|
||||
pub scroll_factor: f32,
|
||||
pub friction: f32,
|
||||
pub pitch: f32,
|
||||
pub yaw: f32,
|
||||
@ -60,6 +61,7 @@ impl Default for CameraController {
|
||||
keyboard_key_toggle_cursor_grab: KeyCode::KeyM,
|
||||
walk_speed: 5.0,
|
||||
run_speed: 15.0,
|
||||
scroll_factor: 0.1,
|
||||
friction: 0.5,
|
||||
pitch: 0.0,
|
||||
yaw: 0.0,
|
||||
@ -75,6 +77,7 @@ impl fmt::Display for CameraController {
|
||||
"
|
||||
Freecam Controls:
|
||||
Mouse\t- Move camera orientation
|
||||
Scroll\t- Adjust movement speed
|
||||
{:?}\t- Hold to grab cursor
|
||||
{:?}\t- Toggle cursor grab
|
||||
{:?} & {:?}\t- Fly forward & backwards
|
||||
@ -99,6 +102,7 @@ fn run_camera_controller(
|
||||
time: Res<Time>,
|
||||
mut windows: Query<&mut Window>,
|
||||
mut mouse_events: EventReader<MouseMotion>,
|
||||
mut scroll_events: EventReader<MouseWheel>,
|
||||
mouse_button_input: Res<ButtonInput<MouseButton>>,
|
||||
key_input: Res<ButtonInput<KeyCode>>,
|
||||
mut toggle_cursor_grab: Local<bool>,
|
||||
@ -120,6 +124,17 @@ fn run_camera_controller(
|
||||
return;
|
||||
}
|
||||
|
||||
let mut scroll = 0.0;
|
||||
for scroll_event in scroll_events.read() {
|
||||
let amount = match scroll_event.unit {
|
||||
MouseScrollUnit::Line => scroll_event.y,
|
||||
MouseScrollUnit::Pixel => scroll_event.y / 16.0,
|
||||
};
|
||||
scroll += amount;
|
||||
}
|
||||
controller.walk_speed += scroll * controller.scroll_factor * controller.walk_speed;
|
||||
controller.run_speed = controller.walk_speed * 3.0;
|
||||
|
||||
// Handle key input
|
||||
let mut axis_input = Vec3::ZERO;
|
||||
if key_input.pressed(controller.key_forward) {
|
||||
|
@ -116,7 +116,12 @@ fn setup_scene_after_load(
|
||||
let mut projection = PerspectiveProjection::default();
|
||||
projection.far = projection.far.max(size * 10.0);
|
||||
|
||||
let camera_controller = CameraController::default();
|
||||
let walk_speed = size * 3.0;
|
||||
let camera_controller = CameraController {
|
||||
walk_speed,
|
||||
run_speed: 3.0 * walk_speed,
|
||||
..default()
|
||||
};
|
||||
|
||||
// Display the controls of the scene viewer
|
||||
info!("{}", camera_controller);
|
||||
|
Loading…
Reference in New Issue
Block a user