Fix apparent jitter in player movement
All checks were successful
CI / Formatting (push) Successful in 1m15s
All checks were successful
CI / Formatting (push) Successful in 1m15s
Remember: `time.delta_secs()` needs to be included in ALL movement calculations!!! This one was pretty well hidden because the player movement was properly interpolated and the camera lerped after it, but it was still barely noticeable!
This commit is contained in:
parent
b42563a94d
commit
d73683359a
1 changed files with 11 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
use avian2d::prelude::*;
|
use avian2d::prelude::*;
|
||||||
use bevy::{
|
use bevy::{
|
||||||
input::mouse::{AccumulatedMouseScroll, MouseScrollUnit},
|
input::mouse::{AccumulatedMouseScroll, MouseScrollUnit},
|
||||||
|
math::curve::EaseFunction::SmoothStep,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -14,19 +15,19 @@ pub fn move_player(
|
||||||
) -> Result {
|
) -> Result {
|
||||||
let acceleration = 500.0;
|
let acceleration = 500.0;
|
||||||
|
|
||||||
let delta_time = time.delta_secs();
|
let delta_v = acceleration * time.delta_secs();
|
||||||
|
|
||||||
if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) {
|
if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) {
|
||||||
velocity.y += acceleration * delta_time;
|
velocity.y += delta_v;
|
||||||
}
|
}
|
||||||
if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) {
|
if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) {
|
||||||
velocity.y -= acceleration * delta_time;
|
velocity.y -= delta_v;
|
||||||
}
|
}
|
||||||
if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) {
|
if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) {
|
||||||
velocity.x -= acceleration * delta_time;
|
velocity.x -= delta_v;
|
||||||
}
|
}
|
||||||
if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) {
|
if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) {
|
||||||
velocity.x += acceleration * delta_time;
|
velocity.x += delta_v;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -39,10 +40,14 @@ pub fn quit(mut exit: EventWriter<AppExit>) {
|
||||||
|
|
||||||
/// Follow the player character with the camera
|
/// Follow the player character with the camera
|
||||||
pub fn move_camera(
|
pub fn move_camera(
|
||||||
|
time: Res<Time>,
|
||||||
mut camera: Single<&mut Transform, (Without<Player>, With<IsDefaultUiCamera>)>,
|
mut camera: Single<&mut Transform, (Without<Player>, With<IsDefaultUiCamera>)>,
|
||||||
player: Single<&Transform, (With<Player>, Without<IsDefaultUiCamera>)>,
|
player: Single<&Transform, (With<Player>, Without<IsDefaultUiCamera>)>,
|
||||||
) {
|
) {
|
||||||
camera.translation = camera.translation.lerp(player.translation, 0.05);
|
camera.translation = camera.translation.lerp(
|
||||||
|
player.translation,
|
||||||
|
SmoothStep.sample_clamped(time.delta_secs() * 15.0),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adjust the camera zoom based on the scroll wheel input
|
/// Adjust the camera zoom based on the scroll wheel input
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue