2D Player Setup (1): Movement and Jumping

I recently started work on this Player script for the 2D mobile adventure game project, and prioritised getting the movement and jumping logic down first. Rather than use Unity’s CharacterController component as we’ve done previously specifically the CharacterController.Move() method and CharacterController.isGrounded boolean), I’m working instead with just a BoxCollider2D and RigidBody2D components.

In particular, I’m taking the player’s horizontal input (multiplied by a speed variable) and changing the RigidBody’s velocity accordingly in order to give the player some movement. The RigidBody’s taking care of gravity, so those variables are on the component rather than in the script this time.

As for the Jumping code, I’ve got some logic in place to check whether the player’s grounded as well as whether we’re in a ‘cooldown’ period or not (just to get around a glitch in this):

The ‘CheckGrounded’ method returns a boolean, and I ended up testing out two solutions for determining whether the player is currently grounded: one which checks the RigidBody’s current velocity on the y axis, and the other which uses RaycastHit2D to cast a raycast down from the player to detect whether they’re on a surface:

The “1 << 8” part essentially just means I’m only looking for collisions with layermask 8 in my project (which I’ve set to be “Ground”). We could also just have a public LayerMask variable, but I really should try to get more comfortable with the bitwise/bit shift operators 😛

Initially I found checking for velocity worked well enough, but later found it caused a few too many bugs (at least for me). I’m opting for the second of these solutions at the moment, and will probably remove the initial once I’m certain I’ll have no use for it.

As you might have noticed, I’ve got some references to a PlayerAnimation custom script, which stores most of the code associated with animating and orienting the player’s sprite appropriately (which is being kept on a child GameObject). I’ll cover the animations side of this in a subsequent article, and I’ll include the full code I’m working with for the Player class below 😉




Game Developer | Game Design and Literature Graduate

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

Recommended from Medium

Application of tree data structure

ExpressJS vs. SailsJS vs. MeteorJS

Finding the REAL shortest path in Javascript: Depth First Search

React + Redux: A Love Story

Live video captioning based on speech

How to host an Angular App to Github Pages ?

How to Build an Analog Clock Using Hooks

Setup a Starport Dapp & Cosmos Full Node with SSL (https/wss) endpoints via Nginx Reverse Proxy…

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
Marcus Ansley

Marcus Ansley

Game Developer | Game Design and Literature Graduate

More from Medium

Journal 130 — Unity App Development, AWS Setup Part 2

Create a Diablo Style Character Controller in Unity!

Enable Ads In Unity!

OnCollisionEnter Vs. OnTriggerEnter — When to use them?