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 physics controls


In this recipe, we show you how to create a custom script to control a game object based on physics. It is not the final controller for the character of our prototype: it's a small piece of code that can be used for a classic 2D shooter, based on physics.

Getting ready

In the Scripts folder in the Project panel, create a new C# script and name it PacktController. Then, double-click on it to open it in Monodevelop.

How to do it...

  1. First we need a bunch of variables to control the forces applied to the GameObject. Add the following lines at the beginning of the script, right below the usual line with the script class declaration:

    (public class PacktController : MonoBehaviour {):
    public float horAcceleration;
      public float cruiseSpeed; //max speed when not pressing
      public float maxSpeed; //max speed while pressing
      public float actualSpeed; //speed at given time
      public float limY; //limit on y, use as mathf.abs
      public float expon; //used to smooth vert movement speed...