Author: Terry Norton
Pub Date: 2013
Size: 18 Mb
For the absolute beginner to any concept of programming, writing a script can appear to be an impossible hurdle to overcome. The truth is, there are only three simple concepts to understand: 1) having some type of information; 2) using the information; and 3) communicating the information. Each of these concepts is very simple and extremely important. These three concepts are combined to access the feature set provided by Unity.
“Learning C# by Developing Games with Unity 3D” assumes that you know nothing about programming concepts. First you will learn the absolute basics of programming using everyday examples that you already know. As you progress through the book, you will find that C# is not a foreign language after all, because you already know the words. With a few keywords and using substitution, before you know it, you’ll be thinking in code.
The book starts by explaining in simple terms the three concepts you need for writing C# code and scripts: 1) variables to hold information; 2) methods (functions) to use the information; and 3) Dot Syntax to communicate the information where it’s needed. The book builds on these concepts to open up the world of C# coding and Unity scripting. You will use this new power to access the features provided in Unity’s Scripting Reference.
The first half of this book is devoted to the code writing beginner. The concepts of variables, methods, Dot Syntax, and decision processing are fully explained. Since C# is an actual programming language, we take advantage of this to develop a State Machine to help control and organize each phase of a Unity project. Once the basic programming concepts are established and we have some State Machine organization, the features and power of Unity are accessed using the Scripting Reference.
The goal of “Learning C# by Developing Games with Unity 3D” is to teach to you how to use the Unity Scripting Reference.
Unity changes script and variable names slightly
When we add our script to a GameObject, the name of our script shows up in the Inspector panel as aComponent. Unity makes a couple of small changes. You might have noticed that when we added LearningScriptto Main Camera, Unity actually showed it in the Inspectorpanel as Learning Script. Unity added a space to separate the words of the name. Unity does this modification to the variable names, too. Notice that the variable number1 is shown as Number 1, and number2as Number 2. Unity capitalizes the first letter as well. These displayed changes improve readability in Inspector.
Placing and using the Player Collider
The Box Collider of Playeris actually moved below Playerso the collider can detect when it collides with the terrain. The Is Triggerproperty is checked. This makes a collider only detect when it collides with another collider instead of bouncing off it.
When it does detect colliding with any other collider such as the terrain or any other GameObject, Playerwill slowly rise up higher to get over it because vertical force is applied to Playerwhenever the collider is triggered.
The collider is extended a bit to the front and rear so as it moves, Playercan rise in advance to go over small changes in the terrain, or other small GameObjects.
Placing and using the Sphere Collider
First thing to notice is that the Sphereisn’t using a Sphere Collider, it’s using a Box Collider instead. Since the Player Box Collideris being used for hovering, I used a Box Collideron the Spherefor Playerto actually collide with other GameObjects, and also to prevent Player from ever falling through the terrain.
Visualizing the completed game
As simplistic as this game is for learning C#, it’s still nice to have some sort of picture of a game when complete. The primary goal is learning how separate pieces of code can come together to form a whole game.
The following screenshot was taken while in PlayStatesScene1_2. Playeris shooting several energy pulses. Of course, your assets will probably look different, but this chapter completes all the coding necessary to have a simple functioning game.
This chapter is going to be structured slightly differently than the previous chapters. Instead of going through step-by-step instructions to add code to our classes and scripts, all the complete files of code can be found in Appendix B. I will simply reference the relevant code when doing a code flow analysis.
Switching to the first play State and playable scene
Chapter 9,Start Building a Game and Get the Basic Structure Running, finished with SetupState. Now we need toswitch to the next State, PlayStateScene1_1. Atthe bottom of the SetupStatescreen is a button Click Here or Press ‘P’ to Play.
Using a State Machine is a design pattern choice
As you discovered, each active State allows you to control what Unity does and what it displays. It organized our codemaking it simple, neat, and easy to follow. I happen to like it. You may have decided that you don’t like the idea of a State Machine after seeing the version in this book. Every programmer develops their own routine for coding. The point is, you will decide what’s easiest for you and establish a pattern that’s comfortable.
Besides showing you why I like State Machines, I also exposed you to writing regular C# classes. I also expanded a little into using a C# interface as well. A C# interface could be used easily with Unity Component classes as well. I just happened to need this C# feature for the State Machine classes.