Files
space-game/src/plugins/follow_cam.rs

29 lines
898 B
Rust

use bevy::prelude::*;
use crate::components::camera::{FollowCam, FollowTarget};
pub struct FollowCamPlugin;
impl Plugin for FollowCamPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (set_cam_target, follow_cam).chain());
}
}
fn set_cam_target(transforms: Query<&GlobalTransform>, cam_target: Single<(&mut FollowTarget, &FollowCam)>) {
let (mut tgt, cam) = cam_target.into_inner();
if let Ok(tgt_transform) = transforms.get(cam.target) {
tgt.pos = tgt_transform.translation();
tgt.rot = tgt_transform.rotation();
tgt.up = tgt_transform.up();
}
}
fn follow_cam(cam: Single<(&mut Transform, &FollowTarget, &FollowCam)>) {
let (mut transform, tgt, cam) = cam.into_inner();
let offset = tgt.rot * Vec3::new(0.0, cam.height, cam.distance);
transform.translation = offset + tgt.pos;
*transform = transform.looking_at(tgt.pos, tgt.up);
}