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.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is JavaScript?

React Quill Editor with full toolbar options and custom buttons (undo & redo)

I built an SPA in an hour and this is what I learned (Part 1)


Introduction to Redux and Mobx

Skateboarding and React Container Components

Which One Is the Best Node.js Framework: Choosing Among 10 Tools

Bitwise Operators in JavaScript and their Applications

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Simon Leen

Simon Leen


More from Medium

Script Communication in Unity Using GetComponent<>

The simplest way to create a First Person Shooter! (Part 1)

Camera Shake in Unity

Adding a thruster speed boost on keypress