Book Image

Learning Unity 2D Game Development by Example

By : Venita Pereira
Book Image

Learning Unity 2D Game Development by Example

By: Venita Pereira

Overview of this book

<p>If you are looking for a guide to create 2D games using Unity, look no further. With this book, you will learn all the essentials of 2D game development by creating five real-world games in a step-by-step manner throughout the course of this book.</p> <p>Starting with a blank scene, you will learn all about the new Unity 2D toolset, which will enable you to bring your scene to life. You will create characters, make them move, create some enemies, and then write code to destroy them. After figuring out all the necessities of creating a game, this book will then assist you in making several different games: games with collision, parallax scrolling, Box2D, and more.</p> <p>By the end of this book, you will not only have created several small games, but you will also have the opportunity to put all your new-found knowledge into creating and deploying a larger, full game.</p>
Table of Contents (17 chapters)
Learning Unity 2D Game Development by Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Controlling the character


We are now going to create a script that will enable us to control our character.

Note

We do not build our scripts until we have added all of them as some of them may need to communicate with each other and we will get errors when they do not exist yet and are therefore unknown. Also, we do not actually need to build the scripts as Unity automatically does this. However, it is good practice as it helps to check for errors.

We will control a spaceship, moving it left and right. The following are the steps to do this:

  1. Download the free public domain spaceship art from the following URL:

    http://freeartsprites.com/free-art/Space-Pack/

  2. We will use the Rocket.png asset as our character sprite.

  3. Create a new sprite GameObject, name it Ship, and assign the Rocket.png file shown in the following image as the sprite:

  4. Create a folder by right-clicking in the Project Browser and going to Create | Folder. Name the folder Code.

  5. Create a new script in our Code folder and go to Create...