animation when moving in any direction
This commit is contained in:
@@ -32,8 +32,14 @@ struct PlayerSpawned {
|
||||
spawned: bool,
|
||||
}
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
struct PlayerMovement {
|
||||
any_direction: bool,
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.init_resource::<PlayerSpawned>();
|
||||
app.init_resource::<PlayerMovement>();
|
||||
app.add_systems(Startup, initial_grab_cursor);
|
||||
app.add_systems(
|
||||
Update,
|
||||
@@ -147,6 +153,7 @@ fn apply_controls(
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<&mut TnuaController>,
|
||||
player: Query<&Transform, With<Player>>,
|
||||
mut controls: ResMut<PlayerMovement>,
|
||||
) {
|
||||
let Ok(mut controller) = query.get_single_mut() else {
|
||||
return;
|
||||
@@ -171,6 +178,10 @@ fn apply_controls(
|
||||
direction += player.right().as_vec3();
|
||||
}
|
||||
|
||||
if controls.any_direction != (direction != Vec3::ZERO) {
|
||||
controls.any_direction = direction != Vec3::ZERO;
|
||||
}
|
||||
|
||||
controller.basis(TnuaBuiltinWalk {
|
||||
// The `desired_velocity` determines how the character will move.
|
||||
desired_velocity: direction.normalize_or_zero() * 8.0,
|
||||
@@ -254,26 +265,16 @@ fn toggle_animation(
|
||||
(&mut AnimationTransitions, &mut AnimationPlayer),
|
||||
With<PlayerAnimations>,
|
||||
>,
|
||||
keys: Res<ButtonInput<KeyCode>>,
|
||||
movement: Res<PlayerMovement>,
|
||||
) {
|
||||
if keys.just_pressed(KeyCode::KeyW) {
|
||||
for (mut transition, mut player) in &mut transitions {
|
||||
transition
|
||||
.play(
|
||||
&mut player,
|
||||
animations.animations[0],
|
||||
Duration::from_millis(100),
|
||||
)
|
||||
.repeat();
|
||||
}
|
||||
}
|
||||
if movement.is_changed() {
|
||||
let index = if movement.any_direction { 0 } else { 1 };
|
||||
|
||||
if keys.just_released(KeyCode::KeyW) {
|
||||
for (mut transition, mut player) in &mut transitions {
|
||||
transition
|
||||
.play(
|
||||
&mut player,
|
||||
animations.animations[1],
|
||||
animations.animations[index],
|
||||
Duration::from_millis(100),
|
||||
)
|
||||
.repeat();
|
||||
|
||||
Reference in New Issue
Block a user