Making Games in Rust - Part 5 - Movement

sbelzile

Sébastien Belzile

Posted on December 20, 2021

Making Games in Rust - Part 5 - Movement

The previous part was about jumps. In there, we learned how to get player inputs and convert it to a vertical movement. This time, let's leverage what we learned to move our player from left to right.

Movement System

Let's start by adding a new system responsible for moving our player.

fn player_movement(
    keyboard_input: Res<Input<KeyCode>>,
    mut players: Query<(&Player, &mut RigidBodyVelocity)>
) {
    for (player, mut velocity) in players.iter_mut() {
        if keyboard_input.pressed(KeyCode::Left) {
            velocity.linvel = Vec2::new(-player.speed, velocity.linvel.y).into();
        }
        if keyboard_input.pressed(KeyCode::Right) {
            velocity.linvel = Vec2::new(player.speed, velocity.linvel.y).into();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This system listens to the left and right keys on our keyboard. When one of these keys is pressed, our system will set our player's linear velocity to + or - the player speed property in the x-axis, and set the y component of the player's velocity to, well, its current y velocity. This ensures that pressing left or right will not have the effect of stopping an ongoing jump.

For our system to work, we still need:

  1. To register it:
.add_system(player_movement.system())
Enter fullscreen mode Exit fullscreen mode
  1. To add a speed property to our player's component:
struct Player {
    speed: f32
}
Enter fullscreen mode Exit fullscreen mode
  1. To set the speed of our player:
.insert(Player { speed: 3.5 })
Enter fullscreen mode Exit fullscreen mode

Meanwhile, I lowered our player's jump_impulse to 7.

If you rung the game, you should be able to make our player move from left or right by pressing the left or right key on your keyboard.

Going Further

Bevy supports multiple types of user input methods. We learned one way of dealing with keyboard events, but the same thing may be done with the mouse, with touchscreens and/or with gamepads.

We also learned to deal with user inputs by looking at the current state of our keyboard. There is another way of doing this: via events. It is important to know that some types of inputs are only provided as events.

You can read Bevy's unofficial cheat book to learn more about the supported input methods and the ways of dealing with them.

Following the Player with the Camera

The player can now move, but the camera will lose sight if the player goes off screen. It would be nice if the camera would follow our player as it moves. One way to do this is to declare the camera as a child of our player:

.with_children(|parent| {
    parent.spawn_bundle(new_camera_2d());
})
Enter fullscreen mode Exit fullscreen mode

We must also remove the old camera from the setup system, and remove the setup system, now that it's empty.

This approach works, but has some caveats. We will learn about a better alternative in part 10 of this tutorial.

The final code is available here.

💖 💪 🙅 🚩
sbelzile
Sébastien Belzile

Posted on December 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related