This course is all about starting to learn how to develop video games using the C# programming language and the Unity game engine on Windows or Mac. Why use C# and Unity instead of some other language and game engine? Well, C# is a really good language for learning how to program and then programming professionally. Also, the Unity game engine is very popular with indie game developers; Unity games were downloaded 16,000,000,000 times in 2016! Finally, C# is one of the programming languages you can use in the Unity environment.
This course doesn't assume you have any previous programming experience. Don't worry if you've never written code before; we'll start at the very beginning and work our way up to building small games by the end of the course. Throughout the course you'll learn core programming concepts that apply to lots of programming languages, including C#, and you'll also learn how to apply those concepts when you develop games.
Computer programming is really fun in general, and programming games is even better!
Caution: Beginning (assuming no prior programming knowledge) is not the same as easy (not hard to do). Learning to program IS hard to do, especially since this course is essentially the first half of a freshman-level college course. Meeting the course challenges while you master the material will be rewarding to you, but doing that will require hard work and maybe even a few expletives along the way.
Module 1: Write your first C# console application and Unity script and learn how we store data in our programs
Module 2: Learn how we use classes and objects to implement our code
Module 3: Learn the basics of Unity 2D games and discover how we make decisions in our code
Module 4: Get and use player input in your Unity games
Module 5: Complete final peer review and take "Final Exam"
“Unity” is a trademark or registered trademark of Unity Technologies or its affiliates in the U.S. and elsewhere.
This course is an independent work and is not sponsored by, authorized by, or affiliated with Unity Technologies or its affiliates

Taught By

Dr. Tim "Dr. T" Chamillard

Associate Professor

Transcript

In this lecture, we'll see how we can use the selection control structure in the timer component from the previous lecture to spawn game objects into our scene from a script. And of course, we're going to spawn teddy bears into our game world in this lecture. So let's go to Unity and get started. Here's our Unity project for spawning teddies. So the main camera, game object, has the teddy bear spawner attached to it and those fields for the prefab teddy bear and the three teddy bears sprites come from over here. There's my teddy bear prefab and there are my three teddy bear sprites. So if I run the game, you will see down here in the game view, that we will periodically have teddy bears spawn and you can see that up in the scene view as well. This is an interesting thing about randomness even though it's randomly picking between these three teddy bear sprite. We've got a number of magenta sprites first and then a few yellow ones and we hadn't gotten any green ones yet. That's just the way random number generation works. So, we will regularly, as game developers, we will fake randomness but actually make things not be totally random. Because if we do, then sometimes it will look wrong even though that's the way random numbers work. Okay, let's go look at the teddy bear spawner script. Here we have a number of fields and if we want fields to be visible in the inspector, we put this square bracket SerializeField annotation above that field. You will find people on the web saying it's easy to make things visible in the inspector, just put public in front of them. But remember, that violates our information hiding that we like in all of the classes that we create. There's no need to break object-oriented good practice here. Just put this SerializeField attribute above any fields you need to be visible in the inspector. So we made the prefab teddy bear field visible in the inspector and we made the three sprites visible in the inspector. For spawn control, we're going to have a min spawn delay of one second and a max spawn delay of two seconds. We're not going to just spawn at exactly the same time every time. We'll have a range of time in which we spawn. We'll have a spawn timer, that timer component that we talked about last time that will tell us when it's time to spawn another teddy bear. I'm making sure we spawn within the screen and so I've included some information that will help me spawn in an appropriate location. In the start method, I save spawn boundaries so I don't have to recalculate those every time I'm going to spawn and then I create a new spawn timer, so I add the component to the game object this script is attached to, which is the main camera. The teddy bears spawner script is attached to the main camera. I set the duration for the spawn timer to be some random number between 1 and 2 and I start running the spawn timer. In my update method, I check to see if the spawn timer is finished. So there's our selection control structure and if it is finished, I'll spawn a bear. We'll see that in a moment. I get a new random duration and I set the spawn timer duration to that new duration instead and I start the spawn timer again. So you should be able to see how this will regularly start the spawn timer when it's done because this becomes true. We'll spawn a bear, get a new duration and start it up again. So we'll just keep spawning teddy bears. The code that spawns a teddy bear is kind of interesting. First, we create a new location and the X is random between the min spawn X and the max spawn X. The Y is random between min spawn Y and max spawn Y. And then you see the Z location for the location and you say, "Wow, that is really crazy." Here's why we're doing it that way. This location is actually a screen location. It's in screen coordinates, it's pixels. And we're going to need to convert that to a location in our game world before we can put our new teddy bear at the location in the game world. And so, if we want a Z in the world to be 0, we set the screen location Z to be this value. So it's relative to where the camera is so that when we do the conversion, it will end up right on the Z equals zero plane, which is where we want everything in our 2D game. Now we convert to world location and we do that by accessing the main camera and then calling a method on that main camera called screen to world point, which you should not be surprised to learn convert the screen point to a world point. And so now world location is in fact a location in the game world, not a location on the screen in pixels. We create a teddy bear and then we set the transform position for the teddy bear to the world location that we just figured out right here. This line right here is the actual spawning and the rest of the stuff we're doing afterwards is adjusting information about the object we just spawned. One of those pieces of information is where it is, and we're also going to set the sprite for the teddy bear to be a random sprite. So we need to change the sprite that the SpriteRenderer is using for the teddy bear. Here's what we do. We have the teddy bear object that we just created. We call get component, which we've seen before on that teddy bear object. We've seen it before when we were getting the rigid body to the component but we can call get component for any component we want and we want the SpriteRenderer. So we put that SpriteRenderer for our new teddy bear into this variable, which is of data type SpriteRenderer. We're going to create a random sprite number between 0 and 3. Now this upper bound when we're calling a random dot range with integers, this upper bound is exclusive and the lower bound is inclusive. So we will get a number that is either 0, 1 or 2 and then we use the selection control structure one more time and we say if the sprite number we just generated is 0, then set the sprite that the SpriteRenderer is using to teddy bear sprite 0. Remember those fields at the very top that we populated in the inspector? If not, if the sprite number is 1, set the sprite to teddy bear sprite 1. And finally, if it's not 0 or 1 then we know for a fact that it's 2, but it would be a waste of time to make this an else if as well. We know at this point that the sprite number is 2, then set the sprite to teddy bear sprite 2. So we learned a little extra stuff in here as we were looking at the spawner about screen coordinates and world coordinates and so on. But, we also saw the selecting control structure here when we were determining which sprite we had selected. And here, where we determined that it was time to spawn a new teddy bear. To recap, in this lecture, we learned how we can use the selection control structure and the timer component to spawn game objects into our scene from.

Explore our Catalog

Join for free and get personalized recommendations, updates and offers.