11use bevy:: { input:: mouse:: MouseWheel , prelude:: * } ;
2+ use avian2d:: prelude:: * ;
23
34pub struct CameraPlugin ;
45
@@ -13,7 +14,9 @@ pub struct MainCamera;
1314impl Plugin for CameraPlugin {
1415 fn build ( & self , app : & mut App ) {
1516 app. add_systems ( Startup , setup_main_camera) ;
16- app. add_systems ( Update , ( update_camera, zoom_camera) . chain ( ) ) ;
17+ app. add_systems ( PostUpdate , update_camera. after ( PhysicsSet :: Sync )
18+ . before ( TransformSystem :: TransformPropagate ) ) ;
19+ app. add_systems ( Update , zoom_camera) ;
1720 }
1821}
1922
@@ -27,27 +30,23 @@ fn update_camera(
2730 mut camera_query : Query < & mut Transform , ( With < MainCamera > , Without < CameraTarget > ) > ,
2831 target_query : Query < & Transform , ( With < CameraTarget > , Without < MainCamera > ) > ,
2932) {
30- //getting camera from query
33+
3134 let Ok ( mut camera) = camera_query. get_single_mut ( ) else {
35+ error ! ( "could not execute query for single MainCamera component" ) ;
3236 return ;
3337 } ;
3438
35- //getting target from query
3639 let Ok ( target) = target_query. get_single ( ) else {
40+ error ! ( "Could not execute query for single CameraTarget component" ) ;
3741 return ;
3842 } ;
3943
40- //Gets the x and y coordinates of our target
44+
4145 let Vec3 { x, y, .. } = target. translation ;
4246
43- //Stores the target x y with our camera z cuz our camera z not changing
47+
4448 let direction = Vec3 :: new ( x, y, camera. translation . z ) ;
4549
46- //Debug statements if we want to use them :)
47- //info!("Camera: {:?}", camera);
48- //info!("Target: {:?}", target);
49-
50- //Make our camera translation match our target translation
5150 camera. translation = direction;
5251}
5352
@@ -62,14 +61,13 @@ fn zoom_camera(
6261 //Amount to zoom in this update cycle
6362 let mut zoom_amount = 0.0 ;
6463
65- //For every event read in this cycle increase the zoom amount by 1 event * sensitivity
64+
6665 for event in scroll_event. read ( ) {
6766 zoom_amount += -event. y * zoom_factor
6867 }
6968
70- //If we have had any mouse scroll events in this update then apply it to the camera projection
69+
7170 if zoom_amount != 0.0 {
72- //extracting the type of projection from camera, only ever going to be Orthographic since 2d but I don't know a better way lol
7371 for mut projection in camera. iter_mut ( ) {
7472 projection. scale += zoom_amount;
7573
0 commit comments