Adding additional functionality to our marble
Let's give the marble a jumping ability so that the player can leap over obstacles. This will be pretty simple, but button input works a little differently than axis input, as you'll see in this section.
Movement code works well in the Update
function, because it updates very slightly for every frame based on the position of the arrow keys or joysticks. However, jumping isn't a gradual movement; it's an immediate command. So, if we were to send a jump command while a button was pressed down, several frames would pass in the time that it took us to press the button and release it, resulting in several jumps sent in quick succession. To remedy this, we'll tell Unity to only send the jump command if the jump button is pressed down and was not pressed down in the last frame. This is done with an input function called GetKeyDown
/GetButtonDown
. In the future, if you ever want to capture button status for every frame regardless of its earlier status...