Skip to content

Commit 5ff21e6

Browse files
committed
smooth movement camera
1 parent 4eed4d0 commit 5ff21e6

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

crates/bevy_dev_tools/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ bevy_render = { path = "../bevy_render", version = "0.18.0-dev" }
3030
bevy_reflect = { path = "../bevy_reflect", version = "0.18.0-dev" }
3131
bevy_time = { path = "../bevy_time", version = "0.18.0-dev" }
3232
bevy_text = { path = "../bevy_text", version = "0.18.0-dev" }
33+
bevy_transform = { path = "../bevy_transform", version = "0.18.0-dev" }
3334
bevy_shader = { path = "../bevy_shader", version = "0.18.0-dev" }
3435
bevy_ui = { path = "../bevy_ui", version = "0.18.0-dev" }
3536
bevy_ui_render = { path = "../bevy_ui_render", version = "0.18.0-dev" }

crates/bevy_dev_tools/src/easy_screenshot.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ use std::time::{SystemTime, UNIX_EPOCH};
55
use std::{fs::File, io::Write, sync::mpsc::channel};
66

77
use bevy_app::{App, Plugin, Update};
8+
use bevy_camera::Camera;
89
use bevy_ecs::prelude::*;
910
#[cfg(feature = "screenrecording")]
1011
use bevy_image::Image;
1112
use bevy_input::{common_conditions::input_just_pressed, keyboard::KeyCode};
13+
use bevy_math::{Quat, StableInterpolate, Vec3};
1214
#[cfg(feature = "screenrecording")]
1315
use bevy_render::view::screenshot::ScreenshotCaptured;
1416
use bevy_render::view::screenshot::{save_to_disk, Screenshot};
15-
#[cfg(feature = "screenrecording")]
1617
use bevy_time::Time;
18+
use bevy_transform::components::Transform;
1719
use bevy_window::{PrimaryWindow, Window};
1820
#[cfg(feature = "screenrecording")]
1921
use tracing::info;
@@ -257,3 +259,46 @@ impl Plugin for EasyScreenRecordPlugin {
257259
);
258260
}
259261
}
262+
263+
/// Plugin to move the camera smoothly according to the current time
264+
pub struct EasyCameraMovementPlugin {
265+
/// Decay rate for the camera movement
266+
pub decay_rate: f32,
267+
}
268+
269+
impl Default for EasyCameraMovementPlugin {
270+
fn default() -> Self {
271+
Self { decay_rate: 1.0 }
272+
}
273+
}
274+
275+
/// Move the camera to the given position
276+
#[derive(Component)]
277+
pub struct CameraMovement {
278+
/// Target position for the camera movement
279+
pub translation: Vec3,
280+
/// Target rotation for the camera movement
281+
pub rotation: Quat,
282+
}
283+
284+
impl Plugin for EasyCameraMovementPlugin {
285+
fn build(&self, app: &mut App) {
286+
let decay_rate = self.decay_rate;
287+
app.add_systems(
288+
Update,
289+
move |mut query: Single<(&mut Transform, &CameraMovement), With<Camera>>,
290+
time: Res<Time>| {
291+
let target = query.1;
292+
query.0.translation.smooth_nudge(
293+
&target.translation,
294+
decay_rate,
295+
time.delta_secs(),
296+
);
297+
query
298+
.0
299+
.rotation
300+
.smooth_nudge(&target.rotation, decay_rate, time.delta_secs());
301+
},
302+
);
303+
}
304+
}

0 commit comments

Comments
 (0)