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 ...

Get Mobile Game Development with Unity now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.