fixed_timestep/fixed_timestep.rs
1//! Shows how to create systems that run every fixed timestep, rather than every tick.
2
3use bevy::prelude::*;
4
5fn main() {
6 App::new()
7 .add_plugins(DefaultPlugins)
8 // this system will run once every update (it should match your screen's refresh rate)
9 .add_systems(Update, frame_update)
10 // add our system to the fixed timestep schedule
11 .add_systems(FixedUpdate, fixed_update)
12 // configure our fixed timestep schedule to run twice a second
13 .insert_resource(Time::<Fixed>::from_seconds(0.5))
14 .run();
15}
16
17fn frame_update(mut last_time: Local<f32>, time: Res<Time>) {
18 // Default `Time` is `Time<Virtual>` here
19 info!(
20 "time since last frame_update: {}",
21 time.elapsed_secs() - *last_time
22 );
23 *last_time = time.elapsed_secs();
24}
25
26fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_time: Res<Time<Fixed>>) {
27 // Default `Time`is `Time<Fixed>` here
28 info!(
29 "time since last fixed_update: {}\n",
30 time.elapsed_secs() - *last_time
31 );
32
33 info!("fixed timestep: {}\n", time.delta_secs());
34 // If we want to see the overstep, we need to access `Time<Fixed>` specifically
35 info!(
36 "time accrued toward next fixed_update: {}\n",
37 fixed_time.overstep().as_secs_f32()
38 );
39 *last_time = time.elapsed_secs();
40}