Cargo.lock
+0
-25
diff --git a/Cargo.lock b/Cargo.lock
index 3209fb1..6a742b9 100644
@@ -664,30 +664,6 @@ dependencies = [
]
[[package]]
name = "bevy_camera_controller"
version = "0.20.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3db746ba06e07836b98405c97cadf661415b2d4d4840ddb766d1b8f980c6d85f"
dependencies = [
"bevy_app",
"bevy_camera",
"bevy_color",
"bevy_curve",
"bevy_derive",
"bevy_ecs",
"bevy_image",
"bevy_input",
"bevy_log",
"bevy_math",
"bevy_picking",
"bevy_platform",
"bevy_render",
"bevy_time",
"bevy_transform",
"bevy_window",
]
[[package]]
name = "bevy_clipboard"
version = "0.20.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1164,7 +1140,6 @@ dependencies = [
"bevy_asset",
"bevy_audio",
"bevy_camera",
"bevy_camera_controller",
"bevy_clipboard",
"bevy_color",
"bevy_core_pipeline",
Cargo.toml
+1
-1
diff --git a/Cargo.toml b/Cargo.toml
index b4236a0..ade815a 100644
@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { version = "0.20.0-rc.1", features = ["dynamic_linking", "free_camera"] }
bevy = { version = "0.20.0-rc.1", features = ["dynamic_linking", "gamepad"] }
# Enable a small amount of optimization in the dev profile.
[profile.dev]
src/camera_controller.rs
+33
-3
diff --git a/src/camera_controller.rs b/src/camera_controller.rs
index 329c281..ad0aa97 100644
@@ -4,9 +4,14 @@ use bevy::{
ecs::{
query::With,
resource::Resource,
system::{Res, Single},
schedule::IntoScheduleConfigs,
system::{If, Res, Single},
},
input::{
ButtonInput,
gamepad::{Gamepad, GamepadAxis, GamepadButton},
keyboard::KeyCode,
},
input::{ButtonInput, keyboard::KeyCode},
math::Vec3,
transform::components::Transform,
};
@@ -28,7 +33,10 @@ impl Plugin for CameraControllerPlugin {
origin: self.origin,
target: self.target,
})
.add_systems(Update, move_camera_with_keyboard);
.add_systems(
Update,
(move_camera_with_keyboard, move_camera_with_controller).chain(),
);
}
}
@@ -38,6 +46,28 @@ struct CameraSettings {
target: Vec3,
}
fn move_camera_with_controller(
settings: Res<CameraSettings>,
mut camera: Single<&mut Transform, With<Camera>>,
gamepad: If<Single<&Gamepad>>,
) {
let mut origin = settings.origin;
camera.translation = origin;
camera.look_at(settings.target, Vec3::Y);
origin += camera.local_z().as_vec3()
* (gamepad
.get(GamepadButton::LeftTrigger2)
.and_then(|r| gamepad.get(GamepadButton::RightTrigger2).map(|l| r - l)))
.unwrap_or_default();
origin += camera.local_x().as_vec3() * gamepad.get(GamepadAxis::LeftStickX).unwrap_or_default();
origin += camera.local_y().as_vec3() * gamepad.get(GamepadAxis::LeftStickY).unwrap_or_default();
camera.translation = origin;
camera.look_at(settings.target, Vec3::Y);
}
fn move_camera_with_keyboard(
settings: Res<CameraSettings>,
mut camera: Single<&mut Transform, With<Camera>>,