From 3725ff43960ded050c7529d2fb0a64ea2c49d6e5 Mon Sep 17 00:00:00 2001 From: Kanabenki Date: Mon, 19 Feb 2024 17:57:20 +0100 Subject: [PATCH] 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. --- examples/helpers/camera_controller.rs | 17 ++++++++++++++++- examples/tools/scene_viewer/main.rs | 7 ++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/examples/helpers/camera_controller.rs b/examples/helpers/camera_controller.rs index 43ea5424747617..75e428aa3e013e 100644 --- a/examples/helpers/camera_controller.rs +++ b/examples/helpers/camera_controller.rs @@ -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