Our character can walk or run at a constant speed, but let’s say we decide the player should be able to go faster by pressing a key. This way, they can choose to move slowly to have more precision or fast to… go faster!
Your task is to add the option to sprint by keeping a key down. Add this to the game, starting from the chapter 2 Godot project.
Your character should:
Which script should this be on? Think about where you should place that code, and write down why that is.
Right now, the player instantly accelerates to the maximum speed, and it stops instantly. Add some acceleration and deceleration on the ground, as in Mario or Sonic games.
Acceleration is already supported, but you will have to do a little extra work to add deceleration.
The purpose of this exercise is to help you think about where to place your code in a Finite State Machine, because it’s not always obvious. You could place the sprint code on the parent move state as it defines the default movement.
That is until you realize that the sprint is something you can only do in the Run state in this exercise, so the code should be on the Run state. But if you could sprint in both the Run and the Air states, depending on the game’s design and the the feel that you want, this code could well be on their parent Move state.