Skip to content

Commit

Permalink
✨ camera follow player
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Nov 10, 2023
1 parent c958e9e commit cf73597
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bevy_rapier2d::prelude::*;
use castles::CastlesPlugin;
use health::HealthPlugin;
use minions::MinionsPlugin;
use player::LocalPlayerPlugin;
use player::{LocalPlayer, LocalPlayerPlugin};
use racks::RacksPlugin;
use teams::TeamsPlugin;

Expand Down Expand Up @@ -50,10 +50,23 @@ fn main() {
})
// --- systems ---
.add_systems(Startup, setup)
.add_systems(Update, cameraman)
.run();
}

fn setup(mut commands: Commands) {
// Camera
commands.spawn((Camera2dBundle::default(), Camera {}));
}

fn cameraman(
mut query_camera: Query<&mut Transform, (With<Camera>, Without<LocalPlayer>)>,
query_player: Query<&Transform, With<LocalPlayer>>,
) {
if let Ok(mut camera_transform) = query_camera.get_single_mut() {
if let Ok(player_transform) = query_player.get_single() {
camera_transform.translation.x = player_transform.translation.x;
camera_transform.translation.y = player_transform.translation.y;
}
}
}
2 changes: 1 addition & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Player {
}

#[derive(Component)]
struct LocalPlayer;
pub struct LocalPlayer;

#[derive(Component)]
struct Hand;
Expand Down

0 comments on commit cf73597

Please sign in to comment.