use bevy::{input::mouse::MouseMotion, prelude::*, window::PrimaryWindow}; use bevy_dolly::prelude::*; #[derive(Component)] struct MainCamera; #[derive(States, Default, Clone, Debug, Eq, PartialEq, Hash)] enum MovementType { #[default] FirstPerson, Free, } fn main() { App::new() .insert_resource(Msaa::default()) .add_plugins((DefaultPlugins, DollyCursorGrab)) .init_state::() .add_systems(Startup, setup) .add_systems( Update, ( Dolly::::update_active, update_camera, update_fpvtype, ), ) .run(); } /// set up a simple 3D scene fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, asset_server: Res, ) { // plane commands.spawn(PbrBundle { mesh: meshes.add(Plane3d::default().mesh().size(5., 5.)), material: materials.add(Color::srgb(0.3, 0.5, 0.3)), ..default() }); commands.spawn(SceneBundle { scene: asset_server.load("poly_dolly.gltf#Scene0"), transform: Transform { translation: Vec3::new(0., 0.2, 0.), ..default() }, ..default() }); let translation = [2.0f32, 2.0f32, 5.0f32]; let transform = Transform::from_translation(Vec3::from_slice(&translation)).looking_at(Vec3::ZERO, Vec3::Y); commands.spawn(( MainCamera, Rig::builder() .with(Fpv::from_position_target(transform)) .build(), Camera3dBundle { transform, ..default() }, )); // light commands.spawn(PointLightBundle { transform: Transform::from_xyz(4.0, 8.0, 4.0), ..default() }); info!("Use W, A, S, D for movement"); info!("Use Shift to go fast"); info!("Use F to switch between Fps or Free camera"); info!("Press Esc to toggle cursor focus"); } fn update_fpvtype( keys: Res>, fps_state: Res>, mut fps_next_state: ResMut>, ) { if keys.just_pressed(KeyCode::KeyF) { let result = if *fps_state == MovementType::FirstPerson { MovementType::Free } else { MovementType::FirstPerson }; println!("State:{result:?}"); fps_next_state.set(result); } } fn update_camera( time: Res