O'Reilly logo

Mobile Game Development with Unity by Paris Buttfield-Addison, Jonathon Manning

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10. Input and Flight Control

Once you’ve got the scene largely laid out, it’s time to start adding the basics of gameplay. In this chapter, we’ll start building the systems that let you get your ship around in space.

Input

There are two different kinds of input used in the game: a virtual joystick, which lets the player provide directional input used for flight, and a button that signals whether the player wants to be firing the ship’s lasers or not.

Note

Don’t forget that the only way to properly test a touchscreen game’s input is by testing it on a touchscreen. To test your game without building to the device, use the Unity Remote app (see “Unity Remote”).

Adding the Joystick

We’ll begin by creating the joystick. The joystick is composed of two visible components: a large square “pad” in the lower-left corner of the canvas, and a smaller “thumb” in the center of that square. When the user places a finger inside the pad, the joystick will reposition itself so that the thumb is directly under the finger, and is still centered. When the finger moves, the thumb will move with it. <<< Follow these steps to start building the input system:

  1. Create the pad. Open the GameObject menu, and choose UI → Panel. Name the new panel “Joystick”.

    We’ll start by making it square, and placing it in the lower-left corner of the screen. Set the anchors to Lower Left. Next, set both the width and height of the panel to 250.

  2. Add the imagery to the pad. Change the Source Image setting ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required