Chapter 9. Start Building a Game and Get the Basic Structure Running

Everything you've learned in this book up to this point has mostly been about getting to know C#, and how to use it at a relatively basic level. Unity has been used more like a helper to explain C#. The next step is to focus more on using Unity's features and let C# play the supporting role. Our goal is to tap into Unity's built-in features using your new ability to write and read C# code, in order to use the variables and methods of Unity's classes provided and documented in the Scripting Reference.

The Scripting Reference provides all the keys to providing GameObjects with action and response behaviors. Over the next two chapters, we will be adding scripts to GameObjects to create a simple game. The game itself is very basic to demonstrate how to use C# and the Scripting Reference to code some of the common game features and GameObject behaviors.

In this chapter, we'll dig into the Scripting Reference some more to write the code for changing levels or Scenes, rotating Player using the Transform class, setting the Player color and specifying number of Lives that Player will have before losing the game. All of this while under the control of the State Machine.

Here's what we'll cover:

  • Unity's documentation
  • Initial game setup and adding a Player GameObject
  • Controlling the Player GameObject

Let's take this thing for a spin...

Easing into Unity's scripting documentation

It's great that Unity has so many classes in the Scripting Reference for us to use in a Unity project. Yet, for a beginner to scripting, even the amount of features available in the Scripting Reference can be overwhelming.

The good news is the Scripting Reference is exactly what the name implies. It's a reference. It's not meant to be a document you have to totally understand before you can begin writing scripts. A reference is a document you search when you need to know how to write the code to perform a specific task.

That's great, right? Sure it is, sort of. As a beginner, how do you know what to search for? This reminds me of school. Ask the teacher how to spell a word and the teacher always responded with "Look it up in the dictionary." Ok, but how do you find out how to spell a word in a dictionary if you don't know how to spell it to find it? I always thought the teacher's answer was rather lame and a waste of energy. I didn't want a definition, just how to spell it.

Let's assume that you wanted to lerp between two vectors. Say what? Now seriously, how in the world would a beginner know words like this in the first place? What the heck is a "lerp"? Would you know to search for that word in the Scripting Reference?

Obviously, most beginners wouldn't know a great deal of the terms, classes, variables, and methods available in Unity. This book is about teaching you the basics of C# so that you can understand code examples presented in the Scripting Reference, and be able to implement similar code in your Unity projects.

Right now, you are at the beginning of your coding education. Learning the fundamentals of C# coding is the easy part, it's like learning how to read and write a sentence.

Now that you can read the C# code examples in the Scripting Reference, it's time to establish a logical approach to access the information it contains. Yes, reading the Unity Manual and at least glancing through the Reference Manual is a requirement. You may understand C#, but now you also have to know how to access the power in Unity, and C# is your key to unlocking that power. The Scripting Reference is only a good reference if you have a basic understanding of what you are looking for, and that knowledge comes from reading the manuals.

Note

The easiest way to view Unity's documentation is to click on Help in Unity's menu bar.

Reading the Unity Reference Manual first

This manual provides a general overview of the main features (classes) you will be accessing while writing your code. A lot of the terminology you need to know is here. At a minimum, read and absorb the terms used and become familiar with the class names in the sections on Physics Components, The GameObject, and Transform Component. You will need to know class names to use the Scripting Reference effectively.

Finding code examples in the Scripting Reference as needed

The Scripting Reference contains all the example code for each class. If you already know what you're looking for, then either do a search for the word or select it from the left panel. Since there are so many classes available, knowing beforehand what you need is what makes reading the Reference Manual a necessity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.206.25