How to update the scale field of OrthographicProjection in Bevy 0.8? #6445
-
I'm new to Bevy and Rust. pub fn spawn(mut commands: Commands) {
let mut camera = OrthographicCameraBundle::new_3d();
camera.orthographic_projection.scale = 5.0;
camera.transform = Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y);
commands.spawn_bundle(camera);
}
pub fn zoom(
mut mouse_wheel_reader: EventReader<MouseWheel>,
mut query: Query<(&Camera, &mut OrthographicProjection), With<LookCamera>>,
) {
for mouse_wheel in mouse_wheel_reader.iter() {
let (_, mut projection) = query.single_mut();
let zoom_scalar = 1.0 - constants::ZOOM_SENSITIVITY * mouse_wheel.y;
let zoomed = projection.scale * zoom_scalar;
projection.scale = zoomed.max(constants::ZOON_MIN).min(constants::ZOON_MAX);
}
} After updating Bevy to 0.8, there is no pub fn spawn(mut commands: Commands) {
commands
.spawn_bundle(Camera3dBundle {
projection: OrthographicProjection {
scale: 5.0,
scaling_mode: ScalingMode::FixedVertical(2.0),
..default()
}
.into(),
transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
} However, I found that I no longer can get the pub fn zoom(
mut mouse_wheel_reader: EventReader<MouseWheel>,
mut query: Query<(&Camera, &mut OrthographicProjection), With<LookCamera>>,
) {
for mouse_wheel in mouse_wheel_reader.iter() {
let (_, mut projection) = query.single_mut(); // The "projection" here never have value.
}
} I tried to modify the query from the above to this: mut query: Query<&mut Projection, With<Camera>> I can get the pub fn zoom(
mut mouse_wheel_reader: EventReader<MouseWheel>,
mut query: Query<&mut Projection, With<Camera>>,
) {
for mouse_wheel in mouse_wheel_reader.iter() {
if let Ok(mut projection) = query.get_single_mut() {
// Try to cast the variant
if let Projection::Orthographic(orthographic) = projection {
// ^ mismatched types
// expected struct `bevy::prelude::Mut<'_, Projection, >`
// found enum `Projection`
}
}
}
} Is there any correct and better way of doing this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In your code snippet, if let Projection::Orthographic(orthographic) = *projection {
// ...
} Additionally, |
Beta Was this translation helpful? Give feedback.
In your code snippet,
projection
is actually aMut<Projection>
, so you need to dereference it:Additionally,
Camera3dBundle
uses a default projection ofProjection::Perspective
, so you will either need to manually set the projection to orthographic or useCamera2dBundle
instead.