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

Space Buddy the alien


We now add the main character in our game, which is the little alien named Space Buddy. To add the alien, we do the following:

  1. Download the alien.png image from the following URL:

    http://freeartsprites.com/platformer

  2. Move it to the sprites folder.

  3. Create a sprite GameObject using the following image:

  4. Name the GameObject SpaceBuddy.

  5. Go to Add Component | Physics 2D | Rigidbody 2D.

  6. Go to Add Component | Physics 2D | Box Collider 2D.

  7. Go to Add Component | New Script and name the script SpaceBuddy.js.

  8. In the Project Browser, add a folder named Scripts and move SpaceBuddy.js into it.

  9. Add the following code to the script:

    Note

    The first line of code is used to ensure strict typing—if you use the same variable for different data types, then an error will be thrown when building the script.

    This prevents bugs related to incorrect data types from occurring. Have a look at the next line of code.

    #pragma strict

    Note

    Next, we define the vertical velocity of the character's jump as shown in the...