-
Hello! :) Right now I have a Bundle for spawning creatures: #[derive(Bundle)]
pub struct Creature {
pub hashmap_animations: HashMapAnimations,
/*
other stuff like health, transform etc.
*/
}
#[derive(Component)]
pub struct HashMapAnimations {
pub current_animation_id: i32,
pub hash_animations: HashMap<i32, SingleAnimation>,
}
pub struct SingleAnimation {
pub id: i32,
pub handle: Handle<AnimationClip>,
pub duration: f32,
pub is_repeatable: bool,
} But when I want to update their animation, I would do : fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// spawn creature 1
let mut hash_animations_1 = creatures::HashMapAnimations::default();
hash_animations_1.add_animation(
1,
asset_server.load("models/creature_1/scene.gltf#Animation1"),
1.58,
true,
);
/* add other animations */
commands
.spawn()
.insert_bundle(creatures::Creature {
hashmap_animations: hash_animations_1,
/*
other stuff
*/
})
.with_children(|parent| {
// bevy 0.7 style
parent.spawn_scene(asset_server.load("models/creature_1/scene.gltf#Scene0"));
});
/* spawn others creatures the same way*/
}
fn update_animation(
mut animation_players: Query<(Entity, &mut AnimationPlayer)>,
mut query_creature: Query<&HashMapAnimations>,
) {
for (e, mut player) in animation_players.iter_mut() {
// how do I know if player is handling animations for creature one or two??
if let Ok(mut map_creature) = query_creature.get_single_mut() {
if let Some(animation) = map_creature.get(ind) {
player.play(animation.handle.clone_weak()).repeat();
}
}
}
} Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
EDIT : see @pinkponk 's answer below Maybe I can clarify my question: from what I understood:
fn start_animation(
mut player: Query<&mut AnimationPlayer>
[...]
) {...}
Am I misunderstanding something? All the examples I found in bevy/examples use only one GLTF scene 😢 Thank you so much! |
Beta Was this translation helpful? Give feedback.
-
I ran into a similar issue, trying to independently set animations for multiple instances of the same scene. The first approach seems the most intuitive, and the eventual solution (traversing the entity hierarchy and creating a special link component) seems kind of obtuse. It makes me wonder if I'm missing something about the design of the |
Beta Was this translation helpful? Give feedback.
-
In my specific case I had two instances of the same animated mesh that I wanted to apply different animations to, rather than two completely different meshes and animations, but I think this solution or something similar will work in general: #[derive(Component)]
struct AnimationIndex {
value: usize,
}
fn init_animation(
animations: Res<Animations>,
mut animation_players: Query<&mut AnimationPlayer>,
animation_indices: Query<(Entity, &AnimationIndex), With<AnimationIndex>>,
children: Query<&Children>,
mut done: Local<usize>,
) {
if *done < animation_indices.iter().len() {
for (animation_index_entity, animation_index) in &animation_indices {
for entity in children.iter_descendants(animation_index_entity) {
if let Ok(mut animation_player) = animation_players.get_mut(entity) {
animation_player.play(animations.0[animation_index.value].clone_weak()).repeat();
*done += 1;
}
}
}
}
} I'm basically cribbing off of the Animated Fox and Update GLTF Scene examples. The |
Beta Was this translation helpful? Give feedback.
EDIT : see @pinkponk 's answer below
Maybe I can clarify my question: from what I understood:
When loaded a scene (
asset_server.load(&scene_path)
) from a gltf file, a new AnimationPlayer is created.If I load two scenes, one for creature 1 and one for creature 2 (different GLTF files, so different animations), two AnimationPlayers will exist in the World
To update the animation of creature 1, I need to query its AnimationPlayer:
But can I know if this AnimationPlayer is the one from creature 1 or 2? Is there a way to mark them?
If I load an
AnimationClip
from creature 1 in theAnimationPlayer
of…