Book Image

Learning C# by Developing Games with Unity 5.x - Second Edition

Book Image

Learning C# by Developing Games with Unity 5.x - Second Edition

Overview of this book

Unity is a cross-platform game engine that is used to develop 2D and 3D video games. Unity 5 is the latest version, released in March 2015, and adds a real-time global illumination to the games, and its powerful new features help to improve a game’s efficiency. This book will get you started with programming behaviors in C# so you can create 2D games in Unity. You will begin by installing Unity and learning about its features, followed by creating a C# script. We will then deal with topics such as unity scripting for you to understand how codes work so you can create and use C# variables and methods. Moving forward, you will find out how to create, store, and retrieve data from collection of objects. You will also develop an understanding of loops and their use, and you’ll perform object-oriented programming. This will help you to turn your idea into a ready-to-code project and set up a Unity project for production. Finally, you will discover how to create the GameManager class to manage the game play loop, generate game levels, and develop a simple UI for the game. By the end of this book, you will have mastered the art of applying C# in Unity.
Table of Contents (20 chapters)
Learning C# by Developing Games with Unity 5.x Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

User input


The first and relatively simple functionality we can add is the ability to jump. We already have basic physics with gravity working on the Player game object. Before we can make our Player game object jump, we need to know when this should happen. The user always needs some sort of interface in order to interact with the game. On the PC and Mac, in most of cases, it will be the mouse or keyboard. On mobile devices, it will be the touchscreen.

Unity gives us a lot of out-of-the-box functions we can call to check whether user is trying to interact through any input.

As we are writing a standalone game, I think it best if we stick to the mouse control.

Please open the Unity Scripting Reference and search for Input. You can have a read through the documentation on the Input class of jump straight to the Input.GetMouseButtonDown public method. Read it thoroughly.

Input.GetMouseButtonDown returns true during the frame when the user pressed the given mouse button. According to the documentation...