Player Setup Part 2
Objective: Set up Player Jump and Speed
Currently the Player moves left and right (without animation) using the left and right/a and d keys. We want to have the Player jump when the space key is pressed.
We start with creating a float for the jump force. We need to keep the script clean so we move the movement logic form the Update method to its own Movement method. Next we run a conditional if to check if the space key has been pressed. We need another variable to this if statement which checks if the Player is on the ground so create a bool function that will return whether the Player IsGrounded.
Inside this we create a RaycastHit2D which checks just below the Player for the ground layer. We null check the RaycastHit2D collider. Because the Players jump happens so fast in a frame we need to create a bool to reset the jump as it will trigger on again making the Player jump twice before registering that the Player is not on the ground. We also need to use a coroutine to set this bool after a short time.
We can now check if the jump can be used along side the if the space key was pressed and then add the jump force to the Players velocity before triggering the coroutine or cooldown period between setting the jump ability on and off.
Currently our Player is moving slowly. We need to increase the speed. We use a private float for the speed and then multiple that by our X-axis/Horizontal input to make the Player faster.