Left/Right Movement with Sprite Flipping in a Unity 2D Platform Game (with JavaScript and C#)

Left/Right Movement with Sprite Flipping in a Unity 2D Platform Game (with JavaScript and C#)

This is part 3 of a series of tutorials for Buiding a 2D Platform Game in Unity.

A character which can jump and double-jump is great, but we need a little more action. In this tutorial, our actor is going to be able to also move left and right. This tutorial shows how to use GetKey to handle the left and right arrow keys for movement. Then we add a function to flip the sprite horizontally, so it appears to face in the expected direction.

This tutorial is presented in two videos: one video each for JavaScript and C# (C-Sharp). I’ve presented both languages, since it’s sometimes not clear how to convert code from the other language when watching a tutorial. The code performs the same functions and produces the same game play, so you only need to watch one.

In this video we…

  • use the JavaScript or C-Sharp (C#) language
  • add left/right movement with arrow keys
  • use GetKey
  • use GetComponent(RigidBody2D)
  • use Vector3
  • add a function to handle sprite flipping, horizontally

JavaScript (Unity Script) version

C# (C-Sharp) version

This tutorial builds on the first project and the GitHub tag for this tutorial is v2

In the next video, I will show how to follow the actor with the camera, and detect death falls.

Be sure to subscribe to the Playlist to get the future updates: Unity 5 2D Platform Game Development (Mac) Playlist

Author: Eric Holsinger

Eric Holsinger is a software professional and photography enthusiast in Southern Maine.

Leave a Reply