♪ [MUSIC] ♪ [NARRATOR] Welcome to Unite Now, where we bring Unity to you,
wherever you are. [BRIAN] Good morning, everybody!
How are you guys doing? Welcome to Unity Learn Live,
Session 4, Part 1: of Create with Code, well actually
we're just doing we're creating the code creating
your first Action RPG with code. Sorry, I didn't say that right.
Welcome, everybody. You do not need to know C#
in this tutorial because Alexei
will take you through [BRIAN] the beginnings of it.
[ALEXEI] Yes, this is our introduction to C# Session. For people who did not follow our first three sessions,
so the last three weeks, we've been doing
an introduction to Unity, an introduction
to basic Unity functions, and then this week, next week,
and the week after, we'll be focusing on an Action RPG with some simple C#
that will let us get a handle on
how we script in Unity, how we use basic scripting tools
to create the action or to structure
the action within Unity. We have a lot to cover today,
I think we're going to move through this content
really quickly. As always, remember,
at the end of the session these will be posted online. So if you miss something,
if you get stuck, if you don't know what you're doing: follow along,
get to the end of the session, and you can go back
and watch from the beginning again. [BRIAN] If you need help,
make sure you hit up the Learners Connect Group
over on the Unity Connect website, the link is right there,
on that bit.ly. Let's get going here. My name is Brian Kenney, and I'm
a Unity Certified Instructor. With me is Alexei Othenin-Girard. Hi! He is going to take you
through all of the steps that we are going
to go through today. Along with us
are some amazing moderators. They are there to answer
your questions in the Q&A. Whatever you have, they will
be able to answer it for you because they are
master programmers. So we have Alejandro, Maor,
James, Nicolas, and Ethan there to help you. So make sure
you ask early and often, and they'll get back to you
as soon as they can. Here's our session outline. Let me take care
of a few housekeeping items. We will be using Unity 2019.3. I will go through the course
outline with you. But you don't need... There are a couple
of questions in the chat or concerns in the chat
if you need to know Unity or C#. You do not.
This is a very gentle approach to programming in a project-based
learning environment that's going to be amazing. Unity follows the Watch, Do,
and Explore model. You'll be doing that a lot today in conjunction
with the project assets. The ideal Zoom layout: make sure
you have the presentation and / or Alexei's steps
in the background. You can put the video on top, and make sure you have
that Q&A panel open in case you got any questions
that you can ask. As always, if this is
the first time you are joining us or the fourth time,
be respectful in the chat. If you want to go back
and do the other recordings, you are more than welcome
to do that, those are amazing. Make sure you download
the Unity Project and follow along or all you have to do
is just watch and you'll learn. Please make sure
that any technical questions, make sure you're putting
that in the Q&A, and general comments
go in the chat panel, it makes it a lot easier
to answer questions. The moderators will answer
your questions a lot easier if you put them in the Q&A. But be respectful and constructive. We are using Unity 2019.3
for this project. Make sure you have a flavor
of IDE that you will be using because we will be editing
some scripts. You see the link
for the assets, there. Alexei will be taking you through
how to create and load the project. I want to reiterate this, make sure
you are kind to the moderators. Q&A: technical questions,
general comments: chat. That takes care
of the housekeeping items, let's move on to our introduction. Let me super quickly say
a couple of things. This is the fourth session
in our beginner series. If you haven't gotten a chance
to see those first three sessions where we worked with an FPS,
a first-person shooter Creator Kit, which was great, James just put the link in the chat
that will take you to the place where all those previous
sessions are recorded. I would say, stay with us
for this session, and then go back
and watch those first three, especially if you are
really new to Unity. If you've never used it before,
those first three videos are a really good tutorial
on just getting your feet wet, how to navigate through Unity, how to manipulate objects in Unity,
et cetera, et cetera. In terms of Watch, Do, Explore:
in this session specifically, I'm warning people now, Brian and I are both
classroom teachers, when we're not working with Unity,
so we have homework for you. We often will say, "Watch us
do this, and you do it on your own, now watch us,
now do it on your own." At the end of the session
we're going to give a challenge for you to take home
and finish on your own. Somebody in the chat
just said, "Boo, homework"... [BRIAN] Boo! Homework!
...which as a teacher I want to say fills me with power, thank you. I know our participants won't be the stereotypical student,
they'll do their homework. [ALEXEI] No, they'll be great.
[BRIAN] They'll do their homework. We are going to do a very basic, hopefully, not too bare-bones, but a ground-level introduction to some of the basic
ideas about code. We'll talk about variables,
what variables are. We'll talk about
what instructions are. What are the instructions
that we're passing to the computer. And then we'll talk about
functions, what functions are and then you guys are going
to write your own function, which is very exciting. Let's get going. First things first, let's download
and set up the Unity Project. This project that we're using is the Creator Kit:
Beginner Code Project. The nice thing about it is
it lives inside of Unity already, so it's very easy to download. I'll show you guys...
This is my Unity Hub, everyone should be seeing
my Unity Hub, here. On the left-hand side,
I have these four tabs, these are my Projects. Learn Projects, which is
what we'll use, Community Projects,
and all of my installs. In this case, I'll click
on the Learn tab. The very first thing in my list, I assume it will be
in everyone's list, is this Creator Kit: Beginner Code. It's got this cute picture
of a cactus and some kind of... moose sheriff person. Click on that, and you'll get
a window that pops up that looks like this, that says
Creator Kit: Beginner Code, has some basic information,
and because I've downloaded it, it says: "Open Project" but yours
will say: "Download Project." Go ahead and click on that. It will
immediately download the project, it will import all of the assets
and expand them. It will actually start
a whole new project for you. Once you've done that,
definitely save your project. You want to make sure
you can get back to it. And it can be easy to dig in,
start making changes, get to the end and realize,
"Oh, I didn't save any of my work." And when you go to close,
you accidentally lose everything. Once you've downloaded the project, first thing you should
definitely do is save it. I'll give people a second. Let me get back to our slideshow presentation. Like I said,
we'll open the Unity Hub, click on that Learn Tab, click on the Creator Kit:
Beginner Code, Download the Project,
and then Open the Project. Should be pretty simple, in terms
of getting everything set up. But while people are doing that, I want to talk about, what do we mean
when we talk about code? What are we actually talking about? Depending on your level
of familiarity with Unity, depending on your level
of familiarity with code in general, again, I'm starting
at a super ground level. People for whom this is very new,
I want to make sure that those people understand
what we're talking about. When we say code,
what we're talking about is a series of instructions
that we pass to a computer that causes that computer
to do something. Very simply, that's the most
basic way to describe it. It's just a set of instructions
that we are giving to the computer. The problem is computers need
those instructions to happen in certain specific ways. Basically, the reason
that we call it a computer language
is we have to learn how to tell the computer
what we want it to do in a way that it will understand. There have been some
really interesting attempts at making what are called natural
language computational languages, where you try to write
in complete sentences and have the computer
understand it. Some of it is really powerful,
some of the projects are not great. In this case, we are using
a language called C#. C# is a very well understood,
very well documented language. There are a lot of resources
for learning it online. It's the language that lives
under the hood in Unity. It's the easiest language
that we can use to talk to Unity. I want to make sure that people
have had a chance to download. I'm seeing a couple
of people raising issues about warning messages
they have gotten during import. In general, I would say anything you can click
through should be fine. I see a couple of people have got their project up
and running, which is great. If you're having problems
with the assets, alternatively, you can go straight
to the Asset Store tab, inside of Unity. You can create a project
and get to them that way. You can go straight
to the Unity Learn website as well. It doesn't matter
what flavor of IDE you use as long as Unity
and that IDE talk to each other. The IDE that is bundled
with Unity is Visual Studio... [BRIAN] Visual Studio.
[ALEXEI] ...which is a very good IDE. So if you don't have anything else,
I would definitely say... Oh! Somebody asked
a great question. [BRIAN] That's a good question.
[ALEXEI] What's an IDE? [ALEXEI] Thank you very much.
[BRIAN] My fault. My bad. IDE stands for Integrated
Development Environment. A big set of words. Basically, an IDE is a text editor. Almost like Notepad or Microsoft Office. Ultimately, an IDE is a way
for us to write the text of the code that we need, the syntax of the code that we need
to pass to the computer. The nice thing about an IDE,
unlike Notepad, an IDE has the semantic rules
of the code that you're writing already built into the IDE, so it can tell you stuff like,
"Hey, you're using this keyword, but you misspelled it." Therefore, your computer
won't understand what it's saying. Or you forgot a semicolon. Or a million tiny mistakes
that programmers, who are human, are definitely prone to making. The IDE will help you by giving you
the tools to see your mistakes and either avoid them,
or fix them quickly, or give you tools
to do things like, "I want to change this variable
name everywhere in my project. Every time I use
this variable name, I want to change it that instead." An IDE can do that for you in
a way that Notepad probably can't. I'm going to answer a couple
of more questions just to make sure that we are
giving people a chance to download this stuff
and get it set up. Somebody says, "I know that Unity
supports JavaScript as well, why is it not used as much?" The fact is that Unity no longer supports JavaScript.
[BRIAN] No longer. We used to be able to write
in a version of JavaScript that was affectionately
referred to as UnityScript. We used to be able
to script in JavaScript, but it is no longer
supported by Unity. So C# is the only language
that Unity supports at this moment. Somebody points out that an IDE
helps you debug your code by stepping through
the code with breakpoints, which we probably won't
get into in this series, but you're absolutely right,
it's a very important utility in an IDE. You can set it up so
that if you are getting an error, you can set the IDE up
to stop running your code when specific things happen. So you can figure out
why you're getting an error and potentially solve it. There's also a question
in there, "Is C# like Java?" That's a tough question to answer, but with most
programming languages, the logic is very similar,
there's just subtle differences. So in Java a for loop
is the same as a C# for loop, same with a foreach loop
or instance variables. All that stuff is very similar between the two languages,
with just subtle differences. [ALEXEI] Subtle
syntactic differences, which is a little frustrating.
[BRIAN] Yeah? [ALEXEI] One of the big things
that divides programming languages is whether or not
they are what's called typed. So Java and C#
are both typed languages. We'll talk about typing
a little bit, but it basically means
that the burden of trying to figure out what
kinds of variables you're using falls to the programmer,
unlike something like JavaScript where you just tell it, "Hey,
I'm going to make a variable," and JavaScript is like, "OK.
You don't even have to tell me what kind of variable.
I'll do whatever you want." I always like writing
in JavaScript, there's like a wide-eyed
Victorian orphan feel to it. Where it's like, "I'll just do
whatever you want. I don't even need to know what
the types of your variables are. I'll just run whatever code
you want me to run." A little bit of a hostage
situation there somehow. OK, if you've gotten
your project up and running, the first thing we'll do
is play the game. Just get a sense
of what this game looks like. So this is my project. And I go here into Scenes, and I click on this ExampleScene. If I double-click
on the ExampleScene, it will run, it will open the ExampleScene,
which is currently open. In order to run my game, I'll press this Play button
up here in the corner. Again, like the FPS
Creator Kit series, we're using a series
of pre-made assets. These are assets that have been
made for this project specifically. And we will use them to help scaffold and structure our investigations with code. I would say, wander around,
grab those health potions. The health potions
will be important. This is just a way for us to
understand what is already there. This project has
already been created. It's already been built for us. We can wander around and see
what has already been made. This should not take you very long, in terms of figuring out
how to move your character around. Click on the environment to move,
click on enemies to attack them. You can hold the mouse button down
to move more smoothly, or you can click
on any specific location, any specific ground spot
and your character, your moose sheriff
will run to that spot. You can see that
when I attack an enemy, I get little damage
pop-ups that appear. This is your standard,
expected action RPG functionality is already in here. Alexei, by the way, a couple
of people might be wondering, what he's doing right now is inside the Assets tab in Unity. You just opened up the Example
Scene that's in the Scenes folder, from the Beginner Code folder
inside the Assets Tab. If you guys can go ahead and open
that up, that would be amazing. Again, the basic unit
of Unity is the Scene. As we develop, we create Scenes,
we fill Scenes with stuff, we save those Scenes so that they have the functionality
that we want them to have, once they have the functionality
we want them to have. Those Scenes are the units
that we use in the Build Menu. For people who remember
a couple of weeks ago, you have to tell Unity
which Scenes you want it to build, and then it builds
everything in those Scenes. So here, all we're doing
is we're saying, "If you double-click
on the ExampleScene icon, what you'll see is the basic Scene that's given to us
in this Project." Hopefully, people are moving
through that fairly quickly. We will move pretty
quickly through this because we have so much
to get through. If you get lost, if you get stuck, follow as much as you can
to the end, and then go back, and you'll be able
to watch the whole video again. Some of the stuff
that we want to get into when we talk about code, some things that are
really important to remember. One: code runs sequentially
from the top of a script down to the bottom of a script. It's really important to remember
when you write code that every line of code
is executed one at a time. Within that, there are ways to use statements
like if statements to say, "If this is true,
run this block of code; if it's not true,
run that block of code." There are pathways
through your code that the program is going to take, but at its base,
we always want to remember that code runs top
to bottom, line by line. Here, we'll get
into the code itself. We're looking at a script
called SpawnerSample. Somebody asked
a really interesting question, which is, "Does the code
change Scene by Scene?" In fact, for a lot of stuff,
yeah, absolutely, it does. In general, the way that Unity likes to work is rather
than writing one huge script that runs your entire Scene, in Unity, it's usually much, much
easier to write many small scripts that are attached
to specific objects that tell those objects
how to behave. That's the model that we use. Instead of writing one big,
long script: "OK, this does this, and this does this and this does
this and this does this," which can be a pain to keep
track of and hard to understand. Instead, we break all
of those functions out usually by object
or by general class structure. You might have a script
that runs all of your scoring. And all your scoring
lives inside the script, and if you have a question about
how does the scoring work, or how do I want to change
the scoring, you go to that script. But individual objects
will have scripts that usually tell them
how to behave in relation to
the other GameObjects. In this case, we're looking
at a script called SpawnerSample. This is the script that spawns the health potions you see
at the beginning of the game. So when you start that level,
you see that there are three health potions that spawn in
right when you start the level. This is the script
that puts them on screen. So we'll talk about the script
and play around with it and see how it works. To do that, I'm back in Unity,
I have to turn off Play mode. I don't want to be in Play mode. I'm looking at Scripts, and I'm looking at
I believe it's Items. Did I get it wrong? Hang on... Scripts > Tutorials, sorry. I'm at Scripts,
I'm looking in Tutorials, and there's this
SpawnerSample script. This is the icon for a script, it's this green pound sign or hashtag, depending
on how old you are. If we double-click that, it will start
opening Visual Studio. As soon as my Visual Studio
starts running, I'll switch over to that. Visual Studio can sometimes
take a little bit to run. Be aware if you feel
like Visual Studio is taking a long time to set up, don't worry, that's pretty normal. And here we go! Here is Visual Studio. Visual Studio is our IDE, our
Integrated Development Environment. This is what it looks like, and this is the
SpawnerSample.cs script. .cs stands for C# script. Here we can see
the breakdown of the script. This is the name of the script. We call the script SpawnerSample, so this is the place
where we define, "Hey, this will be
the SpawnerSample script, and everything that happens
after this curly brace is all in the script." We're not going to talk
about inheritance today, but we see that the SpawnerSample
script inherits from MonoBehaviour, so it has all of the functions,
all of the features that any MonoBehaviour object has. This is a really common technique
when we're writing code where we write more
and more specific scripts that reference or inherit
properties from bigger categories or bigger classes of scripts. MonoBehaviour is kind
of the top category. Anything that derives
from MonoBehaviour is kind of a top-level
script object or GameObject. Alexei, it might be a good idea to touch on the structure
of a program? Yeah, we'll go into this more
when we get back to the code. These are the steps you need to
get your SpawnerSample script open. In general, when we look
at code, at the top, we have our variable definitions. That's telling us what things do we want
to manipulate in this code. Below that, we have
our list of instructions, how do we want
to manipulate those things. When we get back
into this actual code, I'll go more in depth
into what that looks like. Can you go back one slide, Brian? Sure, there you go. Let us know if you're
having trouble getting your Visual Studio
open, et cetera. Variables are one of the most basic
units of code that we work with. A variable gets described
in a lot of ways. The thing you'll hear most often
is a variable is a bucket that holds a value. In other words, a variable
is a name that we assign to a specific value. What happens is when we use the name
of that variable, the computer will grab
that value and replace it. Under the hood,
when we use a variable name, it will look at the value that
we saved inside that variable. In C#, variables always
have to have a type. A type tells the script,
and by extension tells Unity, what kind of variable that is and what sorts of actions
we can do to it. Which actions are legal
and which are not. Let's move forward, and we can show people
a more in-depth breakdown of what this script looks like. Here at the very top of our script, before our Start() function, and I'll break
this down in a second, right here at the top
of the script, right after the script definition, we're creating a GameObject
called ObjectToSpawn. You'll notice if I highlight
ObjectToSpawn, you can see everywhere in the code
where that variable name shows up. All we're saying here is,
"Hey, there's a GameObject, and we're calling it
ObjectToSpawn." In other words, that's the object we want this script to spawn,
that's all we're saying. Unity doesn't know
what that object is. We'll have to go in
and make the association, make it clear what object that is. The script doesn't care. As long as the thing that
we're creating is a GameObject, the script doesn't care,
it could be anything. So all we're saying is,
"Hey, there's going to be an object that we'll want to spawn, we'll
call that object ObjectToSpawn." Then we start what's called
the Start() function. The Start() function is a special
function that Unity gives us that's always accessible
in any script that says, "When this GameObject gets created,
run the stuff that's in here." So everything in Start() runs
when that GameObject gets created. Then we can break this down again. Here, we're declaring
another variable, and we're saying there's
a variable called angle. It is of type int, which means
integer, so it's a whole number. We're setting that variable
equal to the value 15. In other words, the bucket,
the metaphor I was using before, the name of this bucket is angle, and the value
I'm placing in it is 15. I'm pouring that 15 value
into that bucket angle, and anytime angle
shows up in our script, we'll pull that value back out. Here, where we use
this angle value, here, it's going to be equal to 15. But note, down here on line 18,
that angle value changes. This is where we reassign
that angle value. We change the value
of the angle to 55 instead. So make sure you can
find that on line 10, on line 10, make sure that you
can find that int angle = 15. This is an instruction
that's defining a variable and giving it an initial value. When we define a variable, all we need is a type keyword, we need the name of the variable. It is optional to also give it an initial value,
like in this statement. I could just as easily say
int angle semicolon. That semicolon is important. For people who are
really ground-floor, haven't used code before,
that semicolon is really important. That semicolon tells C#,
and by extension, Unity, that this is the end of the line. We're saying this is the end
of the instruction. So at the end of each
of our instructions, you can think of it
as a period in a sentence. It says, "I'm ending this thought,
and I'm starting another one." Also, backing up, you might see
an access modifier that shows whether or not
you can see the variable in Unity, or if it's private,
only inside of that class. That's a good point. Up at the top of the script
in line 6, where it says, public GameObject ObjectToSpawn; thank you, Brian, that public
is an access modifier. That says, "This
ObjectToSpawn variable, I want it to be
accessible by anything, any script or Unity
function anywhere." If I'd said private,
what that would mean is ObjectToSpawn could only be used
inside this specific script. When we use this type
of construction where we say, "Oh, there's a public GameObject," we do that because, as you'll see,
it appears in the Unity Editor. And that's where we can make
the association of, "Oh, this is the thing
we want to spawn." Let's go through this code quickly
and figure out what it's doing. We're making a variable that's
of type integer called angle, we're setting it equal to 15. On the next line,
we're making a variable called spawnPosition
that's of type Vector3, and we're setting it
equal to tansform.position. Now, transform.position might feel
like, "What does that mean?" That just means that we're using
the position of this object. Whatever object
this script is attached to, that's the transform.position
that we're going to be using. Or we're saving transform.position
inside of SpawnPosition. Then, you'll notice
we have blocks of code, where the same thing
gets repeated a couple of times. For people who have
done coding before, for people who are familiar with
how code classes get structured, this is a dead giveaway
because when we write code, we don't want to repeat
our instructions. If you find yourself repeating
the same instruction over and over again, that's always
a sign that instead of repeating that same instruction
over and over again, you need to either find a way
to loop the instructions so it runs multiple times, or write a function
that you can call multiple times. Because it's so much easier
to write, for example, a function, we'll get into what those are in a
second, but they are lines of code that are grouped together
and can be called together. It's much easier to write a function
that you can call multiple times. If you decide
to change the function, it will change everywhere
you want to call it rather than me having to go,
"I fixed this line once, but did I fix it here,
and did I fix it here as well?" If we can, we want to collapse
those into a function, and then just call the function. For now, these are
some pretty complex instructions. You don't need to know
exactly what's going on, but what you do need to understand
is we're developing a direction. At each of these steps,
we're building a set of variables, a set of numbers which we'll use at the end
to do something. We're building a variable
called direction. We'll use
transform.position + direction * 2 to update that
spawnPosition variable. All of these lines, all they're
doing is making specific variables. These three lines,
they just make specific variables. This is the only line
where we call a function. This is the line
where we say Instantiate(), meaning create an object,
create the ObjectToSpawn, at the spawn position with
this rotation: Quaternion.identity. Quaternion.identity means
facing the same direction as its parent object. So this is a complicated way
of saying, "I'm specifying exactly the place and direction that I want
these objects to get spawned." So each time, we specify the place we want them to get spawned,
then we spawn them. Then we specify the place
we want them to get spawned, then we spawn them. Alexei, do you mind
if I add something real quick? [ALEXEI] By all means, absolutely.
[BRIAN] If you guys, If you look at the code
he's showing you right there, programming is a very
pattern-based process. If you look at that, it looks like
there's a pattern going on here, and if you can, like Alexei said, take some of that
pattern-based code, and he's about to take
you through this, your programming journey
will be a lot easier than harder. But it takes a lot of practice to find that pattern, like that. The last thing, some
of the people in the chat might not recognize where
you're getting some of this stuff. Unity has a full
Application Programming Interface or API, or Scripting Reference that you can look up
each one of these... [ALEXEI] Yeah.
[BRIAN] ...each one of these things like Instantiate(), it will
tell you the format of how you need to structure
the Instantiate() function. And we know
it's a function or object because it's a capital "I". So I just wanted to point that out.
You go ahead and take over. I'm just looking at some
of the questions. OK, there's a question
that people are asking which is a very good question,
"What is a Vector3?" I want to put the code back
on screen for a second and talk about what a Vector3 is
because that's really valuable. A Vector3 is... a Unity-specific type. It's a way for Unity
to structure information that specifically refers to the 3-dimensional
coordinates of an object. A Vector3 is always
an X, Y, and Z coordinate kind of put together
which we can then pass to different objects to say,
"Hey, this is your position. This is your Vector3,
this is your position." The Vector3 is just an easy way
for us to encapsulate that position
and direction information into a discrete package that we can then pass
to a bunch of other people. I'm seeing people asking,
"What is a Quaternion?" The easiest way
to think about a Quaternion is a Quaternion is, like a Vector3,
a way to describe how an object is rotated in space. If a Vector3 is where something
is positioned potentially, a Quaternion is where
the object is facing. Let me see, there's a couple
of other good questions. "What does line 15 mean?
And why is there no type for it?" So 15 says we're going to spawnPosition = transform.position + direction * 2 We're taking the direction variable
that we described earlier in the previous line. We're taking the object's
transform.position, the object that this script
is connected to. We're taking the direction
and multiplying it by 2, and we're adding it to the position and saving that position inside
of the SpawnPosition variable. And the reason
that it doesn't have a type is we already gave
the SpawnPosition variable a type when we defined it. We don't need to give it a type
when we use it after that, because its type
has already been determined. You're using the same pattern
with the angle variable as well. I noticed that you're also updating
that value as well. Yeah. Let's see if we can go ahead
and fix a little minor but annoying thing
about this script, which is, we have all these great variables,
and variables are great because if we change them, they'll change everywhere
in the script. But here, specifically,
in this line 15 and then again in line 20
and then again in line 25, we have this *2. That's a way of telling us
how far away from this original object
we'll spawn our health potions. And we might want to adjust that,
but if we want to adjust it, we'll want to adjust
it consistently across all of these objects. So rather than having these three
what are called magic numbers, which means they are just values
whose meaning isn't clear, a better way to do this
is to replace those with variables. So I want to show you guys,
very simply, how to do that. We're going to start
by making a new variable. We'll say this is a variable, it's of type "int,"
so it's an integer, we don't need to worry about
fractional values in this variable. We'll name this variable "radius,"
and we'll set it to "= 5." Then we'll go in here and replace
each of these 2s with "radius." If I decide later,
"I don't want this to be 5, I want this to be 8.
They're too close together, I want their spawn points
to be further apart," then all we need to do
is change that number in one place instead of in multiple places. Remember to save, Ctrl+S,
anytime you change your script. Hopefully, that's a clear example
of how to create a variable, and then add it into your code. This is the list of instructions
for people who want to follow along and are able to do it on their own. All code writing comes down to identifying what
you want to change, figuring out how you
want to change it, and then writing the instructions
that changed the thing you want to change in the way
that you want to change it, which, when it comes down to it,
is quite complicated. People are still in the chat
helpfully telling me more concise definitions
for what a Vector3 is. Joshua says, "It's a way to tell
Unity that we're dealing with X, Y, and Z coordinates instead
of saying each one separately," which I think
is a great way to put it. We're going to zoom forward. We talked about how to create
that radius variable, and then replace
that radius variable in your code. You can go ahead
and do that on your own. Again, if you're falling behind
don't worry about it, we're zooming through.
I just got a 10-minute warning, I hadn't realized
that we're quite so late. Now that we're there, though, hopefully, you've gotten a sense
of how to create a variable. We need a type,
we need a variable name, and, in some cases,
we need a value. In this case,
we're going to use a value. Now we need to think
about how do we want to rewrite this code
so that we have a function instead of having that code
repeat over and over again. The first step is always
that we want to go ahead and back up the script
that we're working on. We can click here to save
the state of this script. It will go ahead and save
a version of this script that we can restore if we need to. That's always a useful thing to do
because sometimes we write code, and it doesn't work
the way we want it to. It doesn't look
the way we want it to, it doesn't function the way
we want it to, or it's broken. If that happens, it's nice
to have a version of that code that we know works
that we can get back to. Now that we've saved our code,
let's talk about what a function is. Functions are one
of the fundamental building blocks of how we write code. There's a huge amount of theory
about what functions are, and how they work, and how they're
best written, et cetera. But effectively,
a function is a series of connected lines of instruction
that get wrapped up together so that we can call them
when we need them, and they'll have
the same basic effect. Some functions take in data
from elsewhere and manipulate it. Some functions take in data,
and then return or spit back out more data. Some functions don't do either
of those things, they act on other parts
of the code themselves or create something
in and of themselves. As you guys can see,
this potion-spawner script, the pattern of... make some variables,
create a potion, make some variables,
create a potion. The fact that it's happening
three times in a row is a warning bell for us
that we could probably rewrite this more easily
as a function. One thing that
we are going to do is we're not going to write
this function for you. This is your homework,
this is your Explore task. It's up to you to write
that function yourself, but I want to give you guys
some sort of tips and the structure
of what that'll look like. So what a function has in it,
the anatomy of a function, it starts with a type,
like a variable declaration. Then there's a function name. Then, and this is important,
there are parentheses, and those parentheses
are the signature of the function. They're the way that Unity knows,
"Hey, this is a function." So function name
and then parentheses. Inside those parentheses, you have
what are called arguments. Arguments are information,
data, code, or variables that we'll pass into the function
when it works. Not every function takes arguments,
not every function needs arguments. When I was rewriting this
potion-spawner function, though, I was like, "This is easier to do
if you have arguments," because it is easier to do
if you can pass into that function
some information. So inside the parentheses
are arguments. Again, this is data that
we'll pass into the function. The curly brace at the end tells us
that the function is starting. Everything that happens
after the curly brace is in the body of the function,
that's the code that's going to execute when we call the function. And then we close the curly brace
to end the function. I'll talk a bit about
what arguments are. This is an empty function, right? I'm just showing you a kickoff hint
of how you might write this function. The type of this function is void. We say void because this function
isn't going to return anything. It won't give us data back out. It will do stuff with data,
it will Instantiate some potions, but we don't need
the function to give us an answer. If it was going
to give us an answer, the type of the function
would be the type of the data it's giving us, but since it's not giving
us an answer, we don't need to worry
about it, it's void. The type is void.
This is the name of the function. I called mine potionSpawner, you
can call yours anything you want. In the parentheses, I'll give it an argument of
type integer called "spawnAngle". Remember, every time
we were running this function, we had to keep changing
this angle variable. This is one of those things where
it's like, "Oh, we set the angle here."
Then we reset the angle here, then we re-set the angle there. That's a good indicator that there's a better way
to do this, there's an easier way to do this. As you're writing your function,
think about if we make that angle variable an argument
when we call that function, we can just pass it the angle that we want it to have
and have it act on that angle. Another way to think
about arguments is they're like little variables
that only live inside the function. But when we tell the function,
"Hey, here's an argument," the function needs to have
that information to run. If you don't pass it
any information when you call it, it won't run. I realize that this is
a lot of ground to cover. Functions are complex,
variables are complex, we're only getting to touch
on this stuff very lightly in the hour that we have. I encourage everyone, play around with
the Beginner Code Project. There are resources online that will take you more
in depth on this stuff. Look up the Beginner Code
Project online, there's some good breakdowns
of variables, variable creation, functions, function creation,
all of that stuff. When you feel comfortable,
go ahead and write your own potionSpawner function. See if you can make it so you write
the potionSpawner function, and then you call
that function four times to spawn four different potions at
four different parts of the game. Don't worry about breaking
anything, you've copied your script so you can always recover
the previous state of your script. Think about where the potions
are being created, and how to make a fourth one. And then when you think your
function's working, go save and play your game.
[BRIAN] I highly recommend that, because there are a couple
of comments in the chat. One person was asking, "How long did it take
for scripting to click?" I highly recommend
when you go through that exploration of that script, you can do a quick
Google search of Vector3, and then put Unity
at the end of it, and it will take you directly
to the Scripting Reference, and you can access
a huge explanation on what Vector3s are in Unity. Same with Instantiate,
you can do the same thing. So go through and look up
each object in that script, so you can learn and really
break that thing down. Yeah, when you have
written your function, we have a place
for you to upload it. You can upload it
to this bit.ly/arpghomework link. And we'll be able to read it. As we go through this series,
a week from today and then two weeks from today,
we'll be able to look at that and see what people
have been building and give you guys feedback. OK, we have a couple of minutes, I'm going to push our time
a couple of minutes, try to answer some questions. People are saying, "I have no idea
what I'm doing with the homework!" I apologize. What I encourage you to do is look up Creator Kit
Beginner Code online. There's a bunch of resources
going more in depth about writing your own function. In general, what
you want to think about is what parts of the function
get repeated. How do we use the parts
of the instructions that we've seen and wrap them up. Instead of writing them
three times, we write once
and then call three times. We'll go over what
functions look like at the beginning
of our next class in a week. I hope this has been
helpful for people. Thank you so much.
We'll see you guys next week. Good luck on the homework
and take care. [BRIAN] Bye, everybody.
[ALEXEI] Bye-bye! ♪ [MUSIC] ♪