-
Hi, I spawn a cube at Startup, then change the cube size at Update(for example follow the mouse cursor update the cube size). I try to use Query, I found how to get Transform through Query, but can not fount how to get Cube through Query in Update system. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's not possible to query for // this
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
..default()
});
// is equivalent to:
let cube_shape = shape::Cube { size: 1.0 };
let mesh = Mesh::from(cube_shape);
let mesh_handle = meshes.add(mesh);
commands.spawn(PbrBundle {
mesh: mesh_handle,
..default()
}); Now that we know what's going on, let's actually change the size of the cube. #[derive(Component)]
struct MyCube;
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
..default()
},
MyCube,
));
fn change_cube_size(mut query: Query<&mut Transform, With<MyCube>>) {
for mut transform in &mut query {
transform.scale = Vec3::splat(2.);
}
} If you want to change the cube size by rebuilding the cube mesh (this is much less efficient), or swap the the cube for another shape entirely, that's also possible. Just replace the mesh handle with a new one. fn change_cube_shape(
mut query: Query<&mut Handle<Mesh>, With<MyCube>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
for mut handle in &mut query {
*handle = meshes.add(Mesh::from(shape::Cube { size: 2.0 }));
}
} |
Beta Was this translation helpful? Give feedback.
It's not possible to query for
Cube
-- it is not a part ofPbrBundle
.PbrBundle
only contains a handle to a mesh asset.Now that we know what's going on, let's actually change the size of the cube.