Show correct speed in hud

This commit is contained in:
Felix Ableitner 2021-01-06 23:37:10 +01:00
parent e0ceb680b4
commit 40805e9775

View file

@ -34,15 +34,17 @@ fn speed_update_system(
rigid_body_set: Res<RigidBodySet>,
mut text_query: Query<&mut Text, With<SpeedText>>,
player_query: Query<&RigidBodyHandleComponent, With<PlayerMarker>>,
time: Res<Time>,
) {
for player in player_query.iter() {
// TODO: would be better to calculate this in a function in player somehow
let player_body = rigid_body_set.get(player.handle());
let player_velocity = player_body.unwrap().linvel();
let player_speed = Vec3::new(player_velocity.x, 0.0, player_velocity.z).length();
let player_velocity_2d = Vec3::new(player_velocity.x, 0.0, player_velocity.z);
let player_speed_2d = player_velocity_2d.length() / time.delta_seconds();
for mut text in text_query.iter_mut() {
text.value = format!("{} m/s", player_speed);
text.value = format!("{:.0} m/s", player_speed_2d);
}
}
}