Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/camera/pan_camera_controller.rs
7223 views
1
//! Example for `PanCamera`, demonstrating basic camera controls such as panning and zooming.
2
//!
3
//! This example shows how to use the `PanCamera` controller on a 2D camera in Bevy. The camera
4
//! can be panned with keyboard inputs (arrow keys or WASD) and zoomed in/out using the mouse
5
//! wheel or the +/- keys. The camera starts with the default `PanCamera` settings, which can
6
//! be customized.
7
//!
8
//! Controls:
9
//! - Arrow keys (or WASD) to pan the camera.
10
//! - Mouse scroll wheel or +/- to zoom in/out.
11
12
use bevy::camera_controller::pan_camera::{PanCamera, PanCameraPlugin};
13
use bevy::prelude::*;
14
15
fn main() {
16
App::new()
17
.add_plugins(DefaultPlugins)
18
.add_plugins(PanCameraPlugin) // Adds the PanCamera plugin to enable camera panning and zooming controls.
19
.add_systems(Startup, (setup, spawn_text).chain())
20
.run();
21
}
22
23
fn spawn_text(mut commands: Commands, camera: Query<&PanCamera>) {
24
commands.spawn((
25
Node {
26
position_type: PositionType::Absolute,
27
top: px(-16),
28
left: px(12),
29
..default()
30
},
31
children![Text::new(format!("{}", camera.single().unwrap()))],
32
));
33
}
34
35
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
36
// Spawn a 2D Camera with default PanCamera settings
37
commands.spawn((Camera2d, PanCamera::default()));
38
39
commands.spawn(Sprite::from_image(
40
asset_server.load("branding/bevy_bird_dark.png"),
41
));
42
}
43
44