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