#Animations management
16 messages · Page 1 of 1 (latest)
just requires a bit of code to change the enum whenever something should change it & something to decide which to play
do you mean State component? Like this?
#[derive(Component)]
enum PlayerAnimationState {
Run,
Idle
}
And how to manage it? With trigger/event? I never use events yet
btw, this is my code now:
https://github.com/temanmd/bevy_tutor_3d
ive never used events before, im quite used to rust on its own so im looking into it now ·::P
for example you can attach a component to your player that contains the index of the current animation and track its change
it is convenient if there is more than one animated entity
in case of single animated entity of course it is easier to call the trigger and intercept it in the observer
You can also create a resource with the index of the current animation and track its change
and how to change that index? For example I have movement_system where change my player translation depends on WASD keys
So I need to mutate that index right there? And how to store it, like State or Resource or Component?
Ofcourse I have more than 1 animated entity (player, monsters etc)
if you have several animated characters you can go the following way
You can create a resource to store all animations as a hashmap
struct AnimationSet {
animations: Vec<AnimationNodeIndex>,
graph: Handle<AnimationGraph>,
}
#[derive(Resource)]
pub struct AllAnimations(HashMap<&'static str, AnimationSet>);
in this case key of hashmap will be "Player" or "Monster" or "Etc"
Then insert component for each animated entity
#[derive(Component)]
pub struct AniData {
pub animation_index: usize,
/// current animation index
pub player_entity: Entity,
/// entity of AnimatiionPlayer component (for convenience)
pub animation_key: &'static str
// key for link with animation set
}
then depends on situation you can update AniData on desired entity
and track it changes
pub fn switch(
mut animation_players: Query<(&mut AnimationPlayer, &mut AnimationTransitions)>,
objects_q: Query<&AniData, Changed<AniData>>,
all_animations: Res<AllAnimations>,
) {
for adata in objects_q.iter() {
if let Ok((mut player, mut transitions)) = animation_players.get_mut(adata.player_entity) {
let ani_set = all_animations.0.get(adata.animation_key).unwrap();
transitions
.play(
&mut player,
ani_set.animations[adata.animation_index],
Duration::from_millis(250),
)
.repeat();
}
}
}
I used this approach in https://github.com/xenon615/bevy_gltf_animator_helper
@viscid kestrel thanks, I thought exactly about this approach
Also I think I need observer (trigger) to catch movement changes (stay/run etc)
You can use a trigger or not, whichever is more convenient
I refactored my movement/animation management code:
animation.rs: https://github.com/temanmd/bevy_tutor_3d/blob/main/src/animation.rs
movement.rs: https://github.com/temanmd/bevy_tutor_3d/blob/main/src/movement.rs
Now next step I want to understand how to refactor the part with WASD keys pressed
It looks very bad 🙂
So maybe I need to move this part from move_player_towards_camera to separate system that will only switch some enum like DirectionState ?
Any thoughts?
I start to rework it here https://github.com/temanmd/bevy_tutor_3d/pull/1