From e7e74457c7f798064882ce8d24c9dd1680b2cd7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Thu, 7 Apr 2022 15:18:20 +0000 Subject: [PATCH] scene viewer improvements: animation reset (#4420) # Objective - Changing animation mid animation can leave the model not in its original position - ~~The movement speed is fixed, no matter the size of the model~~ ## Solution - when changing animation, set it to its initial state and wait for one frame before changing the animation - ~~when settings the camera controller, use the camera transform to know how far it is from the origin and use the distance for the speed~~ --- examples/tools/scene_viewer.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/tools/scene_viewer.rs b/examples/tools/scene_viewer.rs index a66256b80b968..bcb88251705c1 100644 --- a/examples/tools/scene_viewer.rs +++ b/examples/tools/scene_viewer.rs @@ -164,6 +164,7 @@ fn keyboard_animation_control( mut animation_player: Query<&mut AnimationPlayer>, scene_handle: Res, mut current_animation: Local, + mut changing: Local, ) { if scene_handle.animations.is_empty() { return; @@ -178,11 +179,20 @@ fn keyboard_animation_control( } } - if keyboard_input.just_pressed(KeyCode::Return) { + if *changing { + // change the animation the frame after return was pressed *current_animation = (*current_animation + 1) % scene_handle.animations.len(); player .play(scene_handle.animations[*current_animation].clone_weak()) .repeat(); + *changing = false; + } + + if keyboard_input.just_pressed(KeyCode::Return) { + // delay the animation change for one frame + *changing = true; + // set the current animation to its start and pause it to reset to its starting state + player.set_elapsed(0.0).pause(); } } }