Skip to content

Commit cce778d

Browse files
committed
feat: apply damping along XZ plane, e.g. deceleration
1 parent 6cfcea6 commit cce778d

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/character_controller/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl Plugin for CharacterControllerPlugin {
110110
)
111111
.run_if(in_state(InGameState::Playing)),
112112
)
113+
.add_systems(Update, apply_movement_damping)
113114
.add_systems(
114115
OnEnter(InGameState::PlayerDead),
115116
handle_player_dead_velocity,
@@ -156,13 +157,6 @@ fn handle_keyboard_input_for_player(
156157
local_velocity.z += 1.0 * speed;
157158
}
158159

159-
let world_velocity = player_transform.rotation * local_velocity;
160-
161-
movement_action_writer.write(MovementAction {
162-
direction: MovementDirection::Move(world_velocity),
163-
character_controller_entity: player_entity,
164-
});
165-
166160
if local_velocity.x == 0.0 && local_velocity.z == 0.0 {
167161
if movement_state.0 != MovementStateEnum::Idle {
168162
movement_state.0 = MovementStateEnum::Idle;
@@ -177,6 +171,17 @@ fn handle_keyboard_input_for_player(
177171
}
178172
}
179173

174+
if local_velocity == Vec3::ZERO {
175+
return;
176+
}
177+
178+
let world_velocity = player_transform.rotation * local_velocity;
179+
180+
movement_action_writer.write(MovementAction {
181+
direction: MovementDirection::Move(world_velocity),
182+
character_controller_entity: player_entity,
183+
});
184+
180185
if keyboard_input.just_pressed(KeyCode::Space) {
181186
movement_action_writer.write(MovementAction {
182187
direction: MovementDirection::Jump,
@@ -223,10 +228,6 @@ fn handle_movement_actions_for_character_controllers(
223228
let Ok(direction_from_world_velocity) =
224229
Dir3::new(world_velocity)
225230
else {
226-
// can also be happen when velocity is Vec3::ZERO but i kinda dont want that
227-
// because if velocity is zero there should be no message written. could save a
228-
// bit of perf i guess
229-
warn!("Setting zero velocity");
230231
velocity.x = 0.0;
231232
velocity.z = 0.0;
232233
return;
@@ -344,3 +345,11 @@ fn apply_gravity_over_time(
344345
}
345346
}
346347
}
348+
349+
// Apply damping in the XZ Plane, basically this is deceleration over time
350+
fn apply_movement_damping(query: Query<&mut LinearVelocity>) {
351+
for mut velocity in query {
352+
velocity.x *= 0.9;
353+
velocity.z *= 0.9;
354+
}
355+
}

src/enemy/ai/systems.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ pub fn handle_chasing_enemies(
156156
for (agent_desired_velocity, agent_enemy_entity_pointer) in
157157
enemy_agents_query
158158
{
159+
if agent_desired_velocity.velocity() == Vec3::ZERO {
160+
continue;
161+
}
162+
159163
let Ok((enemy, entity, mut velocity)) =
160164
enemy_query.get_mut(agent_enemy_entity_pointer.0)
161165
else {

0 commit comments

Comments
 (0)