Added system that makes objects move based on their velocity.

This commit is contained in:
2025-12-24 10:48:52 +01:00
parent 4ebff662ac
commit 384ea0f7a1

View File

@@ -1,4 +1,5 @@
use bevy::color::palettes::basic::RED;
use bevy::log;
use bevy::prelude::*; // Core Bevy types
@@ -6,6 +7,7 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, velocity_timestep)
.run();
}
@@ -15,7 +17,16 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
let shape =meshes.add(Circle::new(50.0));
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(100.0, 0.0, 0.0)));
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(100.0, 0.0, 0.0), Velocity(vec3(0.0, 100.0, 0.0))));
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(0.0, 0.0, 0.0)));
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(0.0, 0.0, 0.0), Velocity(vec3(0.0, 100.0, 0.0))));
}
#[derive(Component)]
struct Velocity(Vec3);
fn velocity_timestep(time: Res<Time>, mut query: Query<(&Velocity, &mut Transform)>) {
for (velocity, mut transform) in query.iter_mut() {
transform.translation += velocity.0 * time.delta_secs();
}
}