diff --git a/examples/follow_target.rs b/examples/follow_target.rs new file mode 100644 index 0000000..856c93b --- /dev/null +++ b/examples/follow_target.rs @@ -0,0 +1,97 @@ +//! Demonstrates how to have the camera follow a target object + +use bevy::prelude::*; +use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin}; +use std::f32::consts::TAU; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugins(PanOrbitCameraPlugin) + .add_systems(Startup, setup) + .add_systems(Update, (animate_cube, cam_follow).chain()) + .run(); +} + +#[derive(Component)] +struct Cube; + +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // Ground + commands.spawn(PbrBundle { + mesh: meshes.add(shape::Plane::from_size(5.0).into()), + material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), + ..default() + }); + // Cube + commands + .spawn(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(0.0, 0.5, 0.0), + ..default() + }) + .insert(Cube); + // Light + commands.spawn(PointLightBundle { + point_light: PointLight { + intensity: 1500.0, + shadows_enabled: true, + ..default() + }, + transform: Transform::from_xyz(4.0, 8.0, 4.0), + ..default() + }); + // Camera + commands.spawn(( + Camera3dBundle { + transform: Transform::from_translation(Vec3::new(0.0, 1.5, 5.0)), + ..default() + }, + PanOrbitCamera { + // Panning the camera changes the focus, and so you most likely want to disable + // panning when setting the focus manually + pan_sensitivity: 0.0, + // If you want to fully control the camera's focus, set smoothness to 0 so it + // immediately snaps to that location. If you want the 'follow' to be smoothed, + // leave this at default or set it to something between 0 and 1. + pan_smoothness: 0.0, + ..default() + }, + )); +} + +#[derive(Default)] +struct AnimateAngle(f32); + +/// Move the cube in a circle around the Y axis +fn animate_cube( + time: Res