fix camera angles to prevent char from disappearing

This commit is contained in:
2025-06-24 17:42:36 +02:00
parent 6e23d12655
commit 05d32c943d

View File

@@ -30,12 +30,17 @@ struct CameraUi;
pub struct MainCamera { pub struct MainCamera {
dir: Dir3, dir: Dir3,
distance: f32, distance: f32,
target_offset: Vec3,
} }
impl MainCamera { impl MainCamera {
fn new(arm: Vec3) -> Self { fn new(arm: Vec3) -> Self {
let (dir, distance) = Dir3::new_and_length(arm).expect("invalid arm length"); let (dir, distance) = Dir3::new_and_length(arm).expect("invalid arm length");
Self { dir, distance } Self {
dir,
distance,
target_offset: Vec3::new(0., 2., 0.),
}
} }
} }
@@ -57,7 +62,7 @@ pub fn plugin(app: &mut App) {
fn startup(mut commands: Commands) { fn startup(mut commands: Commands) {
commands.spawn(( commands.spawn((
Camera3d::default(), Camera3d::default(),
MainCamera::new(Vec3::new(0., 2.5, -13.)), MainCamera::new(Vec3::new(0., 1.8, -15.)),
CameraRotationInput::default(), CameraRotationInput::default(),
)); ));
} }
@@ -120,14 +125,14 @@ fn update(
return; return;
} }
let target = target_q.translation;
let arm_tf = arm_rotation; let arm_tf = arm_rotation;
let Ok((camera, mut cam_transform, cam_rotation_input)) = cam.single_mut() else { let Ok((camera, mut cam_transform, cam_rotation_input)) = cam.single_mut() else {
return; return;
}; };
let target = target_q.translation + camera.target_offset;
let direction = arm_tf.rotation * Quat::from_rotation_y(cam_rotation_input.x) * camera.dir; let direction = arm_tf.rotation * Quat::from_rotation_y(cam_rotation_input.x) * camera.dir;
let max_distance = camera.distance; let max_distance = camera.distance;