Book Image

Mastering Unity 2D Game Development - Second Edition

By : Ashley Godbold, Simon Jackson
Book Image

Mastering Unity 2D Game Development - Second Edition

By: Ashley Godbold, Simon Jackson

Overview of this book

The Unity engine has revolutionized the gaming industry, by making it easier than ever for indie game developers to create quality games on a budget. Hobbyists and students can use this powerful engine to build 2D and 3D games, to play, distribute, and even sell for free! This book will help you master the 2D features available in Unity 5, by walking you through the development of a 2D RPG framework. With fully explained and detailed C# scripts, this book will show you how to create and program animations, a NPC conversation system, an inventory system, random RPG map battles, and full game menus. After your core game is complete, you'll learn how to add finishing touches like sound and music, monetization strategies, and splash screens. You’ll then be guided through the process of publishing and sharing your game on multiple platforms. After completing this book, you will have the necessary knowledge to develop, build, and deploy 2D games of any genre!
Table of Contents (20 chapters)
Mastering Unity 2D Game Development - Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface

Coding with components


Components in Unity are the building blocks of any game; almost everything you will use or apply will end up as a Component on a GameObject in a scene. When a GameObject is selected, you can view each of its Components from its Inspector.

You will find that you need to access these various components with script. Next we will discuss the various ways on which you can do this.

Accessing components

To reference the components of a GameObject from within your code, you need to use the GetComponent function. The following shows you examples of how this is achieved:

Rigidbody myScriptRigidBody; 
void Awake() 
{  
  var renderer = this.GetComponent<Renderer>(); 
  var collider = renderer.GetComponent<Collider>(); 
  myScriptRigidBody = collider.GetComponent<Rigidbody>(); 
} 
void Update() 
{ 
  myScriptRigidBody.angularDrag = 0.2f * Time.deltaTime; 
} 

This way, the Rigidbody object that we want to affect can simply be discovered once (when the scripts awakes...