Book Image

Unity 2D Game Development Cookbook

By : Claudio Scolastici
Book Image

Unity 2D Game Development Cookbook

By: Claudio Scolastici

Overview of this book

<p>Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games.</p> <p>Unity 2D Game Development Cookbook is a practical guide to creating games with Unity. The book aims to serve the purpose of exploring problematic concepts in Unity for 2D game development, offering over 50 recipes that are easy to understand and to implement, thanks to the step-by-step explanations and the custom assets provided. The practical recipes provided in the book show clearly and concisely how to do things right in Unity. By the end of this book, you'll be near "experts" when dealing with Unity. You will also understand how to resolve issues and be able to comfortably offer solutions for 2D game development.</p>
Table of Contents (15 chapters)
Unity 2D Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Coding a scrolling background


We will keep going with the same game scene we left at the end of the previous recipe, so just stick with the instructions!

How to do it...

  1. In the Scripts folder of your Project panel create a new C# script and name it Back_Manager.

  2. Double-click on the script to open it in Monodevelop.

  3. Let's begin by creating the required variables: we need one public Transform variable to store the reference to the prefab to be used as the background panels and a few private vars to define things like the scale of the panels, the distance from the character, and a reference to the game character.

    From a game logic perspective, the most important variable we are adding to the script is an Array[] variable type that we use to manage the three panels that repeat endlessly in the background. To achieve that, add the following lines at the beginning of the script:

    public Transform backBrick;
    
    private Transform[] backArray=new Transform[3]; 
    private Transform thisChar;
    private float distance...