From 63fb5c22c9f10aef296537f2dd02bf7b87001f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Thu, 25 Nov 2021 18:54:07 +0100 Subject: [PATCH] add new system to be able to move the camera --- examples/ios/src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/examples/ios/src/lib.rs b/examples/ios/src/lib.rs index eff06174682d2..e7b15c5984aa7 100644 --- a/examples/ios/src/lib.rs +++ b/examples/ios/src/lib.rs @@ -32,8 +32,37 @@ fn main() { .add_plugin(AudioPlugin) .add_startup_system(setup_scene) .add_startup_system(setup_music) + .add_system(touch_camera) .run(); } + +fn touch_camera( + windows: ResMut, + mut touches: EventReader, + mut camera: Query<&mut Transform, With>, + mut last_position: Local>, +) { + for touch in touches.iter() { + match touch.phase { + TouchPhase::Started => *last_position = None, + _ => (), + } + if let Some(last_position) = *last_position { + let window = windows.get_primary().unwrap(); + let mut transform = camera.single_mut(); + *transform = Transform::from_xyz( + transform.translation.x + + (touch.position.x - last_position.x) / window.width() * 5.0, + transform.translation.y, + transform.translation.z + + (touch.position.y - last_position.y) / window.height() * 5.0, + ) + .looking_at(Vec3::ZERO, Vec3::Y); + } + *last_position = Some(touch.position); + } +} + /// set up a simple 3D scene fn setup_scene( mut commands: Commands,