Hello again.
In this lecture we are having a very high level overview of what methods are and we're just going to
make a capsule spin around by using the transform rotate method.
Okay, let's get started.
Hello and welcome.
We're going to start this lecture off with a challenge right from the get go.
I want you to start a new 2D project in Unity.
So open up Unity Hub, start yourself a new project.
Call it whatever you like.
Mine will be called delivery driver, but you can call it whatever you like.
And then once the project is open, want you to add a capsule sprite, and that's going to be our car.
And then I'd like you to create a C-sharp script or call driver, and then I'd like for you to add the
script to your capsule sprite as a component.
So if you're not exactly sure how to do that, we covered that in the very first intro section, but
it's a case of adding that to your Sprite.
And then last step, rename the capsule sprite to give you a car, a nifty name.
Mine's going to be called Cruisy MC Drive.
You can call yours whatever you like.
There's your challenge.
Pause the video and then start the video again once you've completed those steps.
Hi.
Okeydokey.
So I'm in Unity Hub.
I'm going to click on New.
I'm using 20, 21.1, etc. I'm going to click on to DX, rename my project to be called Delivery Driver
and Repos Unity two DX.
Yep, that's exactly where I want it.
Click the create button.
Wait a moment, make myself a cup of tea.
Go for a walk around the block and when I get back, unity hopefully will be good and ready to go.
Okay.
Welcome back.
I had a nice little walk around the neighborhood, did some push ups, and here I am ready to roll a
little bit out of breath.
But good for action.
I'm going to jump over into my hierarchy right click and select 2D object sprites and find a capsule.
I'm going to rename it right now.
Actually, I'm going to do the challenge a little bit.
Out of order like a rebel.
What was he called?
Cruisy Muck drive.
Kind of makes me want to eat fast food, but hopefully I'm not going to get sued.
That's probably far enough away from copyright, I think, to be able to call it MC Drive.
What are we doing next?
We're creating ourselves a script, so right click down in the assets area, create C-sharp script.
We will call this driver.
Now the last step was to click on Cruisy MC Drive and then drag over driver.
I could have also clicked on add component and searched for driver in there and that would have done
the same job.
Now it's time for a little bit of theory.
Okay.
We're going to talk about methods.
What are methods?
Well, methods are also called functions.
So you might hear a lot of people referring to them as functions.
Technically, technically, they're methods, but they're kind of pretty much interchangeable.
Those two words when it comes to the way that we're using it in unity and C sharp methods execute blocks
of code that makes our game do things.
So methods do stuff essentially and we can use the methods already available in unity that some hardworking
programmer in Unity headquarters has created for us to use.
Or we can create our own methods and we'll be doing plenty of both of these throughout the entire course.
The other thing I want you to know in this very, very quick intro to methods is that we do creating
and calling.
So when we create a method, we're giving it a name and saying all the things that it should do.
So a method might be make the thing move forward, make the thing turn around.
And we also call a method, and that is when we are saying, do the things now, please, let's jump
back into our game and I'm going to open up my driver script, double click on that.
That should open up Visual Studio code.
Just going to tidy things up a little bit here.
Let's see, I'm going to full screen this actually and make it a little bit bigger.
So while we're learning things, we're taking up the entire screen just for a moment.
And you can see right at the very start, we've got our using these are namespaces.
We'll get to that later on.
But basically it's saying, hey, you can use all of the things that are contained within here that
the the unity engineers have given for us.
We've got public class classes as a way of grouping similar things together.
And within particular classes we have particular methods we can execute.
And then we get to the stars of the show, start and update.
We also have in front of START we have some comments in here.
You can tell it's a comment because it's in green, but also it has a double forward slashes at the
start.
You can do any old comments, you want to hit double fours slash and say this is staff.
Yay.
So that's a way to create comments.
Pretty handy in this case.
We don't need these particular comments, so I'm just going to delete them for the moment just so we
can keep this as clean as possible.
But in general, I don't bother.
I just leave them in there.
Okay, so start and update.
These are methods in particular, they're callbacks.
Now, we don't need to dig into the depth of what callbacks are, but what I'd like you to understand
at the moment is unity.
The unity engine and the unity system of how it executes the game is going to call, start and update
a little bit in the black box.
So somewhere Unity is saying, hey, do all the things in start and it's saying do all the things in
update.
Now when does it call that?
When does it say you do all the things that start?
It's just when we click on the play button, we click on Play Boink, and then anything that's in start
on any scripts that we have in our scene, it will execute, it will run and say, Do these things now
and then we have an update.
Update is called or update is happening or update is doing its stuff, if you will.
Every single frame that our game is running and we have lots of frames that are running every single
second.
So let's jump back into Unity and have a look at what we'd like to do here.
Now I'm going to click on Cruzi hit W to move Cruzi to the side because I don't like that he's sitting
right underneath the camera here.
And what are we going to do in this lecture is rotate cruzi using code.
And if you have a look, we've played around this before, we've got our position up, down, left,
right, and we've also got our rotation.
We want to be rotating on the Z or the Z axis.
How do I know that?
Well, I went to X and played around.
I went to Y and played around and went to Z and I'm going to call it Z and played around.
And Z was the one who who did it.
The other way you can look at it is say, well, if we are rotating around this green, it's going to
rotate around that way.
If we're rotating around this red or rotate that way, Z is going into the world and it's going to rotate
around Z.
How do I know it goes into the world?
How can we see that?
Well, a little sneaky trick for you here.
Up in our scene window, you see a 2D button.
If you click on that, it will toggle from 2D to 3D.
I'm going to hold down old and my left mouse button, which will allow me to rotate around.
And you can see that we have this Z or Z axis that's going into our world.
And if we rotate around that.
So I'm going to hit my E key.
You can see I'm rotating on the blue which rotates around the Z and also just a little sneakiness that
in Unity two DX games are basically just 3D games where we lock the camera by clicking this 2D button
here and we are only moving things around on the X and the Y axis.
Okay.
The next thing I'll show you is at the moment, if I was to rotate, cruisy, rotate like this, my
widget is staying at the worlds y up direction and the world's x direction.
I'm going to undo that move if I come up to the top here and see where it says global.
If I click from global to local now as I rotate, you'll see that the Green Arrow is staying forwards
relative to our object because we've rotated the object around.
The object says, Well, my forwards is now this direction.
If we rotate around a bit further says, Oh, my forward is that direction.
That's kind of handy concept for us to have just so we can see which way our car is facing as we drive
things around.
So you can see I'm rotating things around here just by clicking and dragging, changing the number in
here if you will change it.
Say 45 bump puts on 45 degree angle.
How do we do that in code?
Let's return it to zero.
Jump back over into our script.
And in here I'm going to do this in code by using a method.
Okay.
Which method?
Well, this method is going to relate to the transform component.
So I'm going to start by typing in transform with a lowercase t dot.
And now you see Visual Studio is saying to us, Hey, here's a whole bunch of things that you get if
you're trying to use something within the transform class.
So this is a class and we'll talk about classes more later on.
But we're using the transform class.
It's also relating to this transform component just here.
So in transform component, we can do things to position things for rotation, things to scale.
We want to do something to the rotation.
And there's a method that comes with unity already, which is called rotate.
So every time transform, rotate and methods need parentheses at the end of them.
So these brackets, these round brackets are called parentheses.
So transform, rotate, and then at the end is a semicolon.
Now that's pretty good, but it's missing something because what it needs within these parentheses is
it's saying, Hey, how do you want me to rotate?
What do you want me to do?
So this particular method is asking for some arguments.
It wants to know how much you rotating and it wants to know how much we're rotating on the X, the Y
and the Z.
So in here we'll say, Well, for example, let's rotate nothing on the X, let's rotate nothing on
the Y and let's rotate, say 45 on the Z.
So that's us calling the rotate method that exists already in unity and putting that with inside the
start method, which unity is calling all on its very own.
It's saying, I'll do start when you push the play button.
Rick No worries.
And so let's save this up.
Make sure you save you don't have a dot up here.
You've got the X back into unity.
And to see this more clearly, I'm going to grab my game window, hold my left mouse, button down,
drag it over to the side so that we have the game window and the scene window open at the same time.
That'll just allow us to see more things, just realign it a little bit, move the cameras around a
little bit.
All good.
Now, when I click on play, what do we expect to have happen?
Look, it's rotated 45 degrees and you can see our value up here is 45.
So I'm going to click play to stop proceedings.
So now we've done that in START.
That's cool.
The other thing I want to show you is we can put this exact same thing into update.
An update is getting cold, like I said, every single frame.
So let's highlight our transform rotate.
I'm going to control X to cut that, I'm going to jump down into update, I'm going to control V to
paste that.
And this now will be rotating 45 degrees every frame that our computer is able to calculate per second.
Let's save that back over into unity.
Now, there's a little thing I want to show you here.
It might be a little bit hidden on your game window, but if you drag your game window in and find stats,
just click on stats.
You'll see at the moment we're getting 743 frames per second.
Well, you know, I've got a pretty quick computer, but the computer's not really doing anything at
the moment, so don't celebrate too much.
Let's click on play and we'll see what happens.
Okay?
Whoa, it's out of control at spinning.
Really quick.
We have.
I'll just pause.
You can pause by the little pause button next to the play button 337 frames per second that we are moving
45 degrees, which is obviously really, really quick and a bit quicker than we want to.
So let's jump back over into our script and we'll change from 45 to 0.1.
And because we've got a decimal place in here and because this is a float, we need to put F at the
end.
So Unity knows, oh, it's a float and it doesn't get it confused with something else.
Now let's save back over into unity.
Click on the play button and now our object is moving at a more sensible pace.
But we're using.
A method to make our object rotate around, which is super cool.
I'm going to play.
I'm going to click on stats to get rid of that, put my window back in a sensible position.
Just a reminder here, we've played around with START, we've played around with Update.
Both of these are methods that are being called as callbacks by unity, and we ourselves are calling
the rotate method and we're adding in these particular parameters to make our thing rotate around.
So there we go.
There's a quick introduction to methods.
I will see you in the next lecture.