Hello again.
In this lecture, we're going to be adding some variables so that we can play around with our numbers
more easily and make our object change its behavior.
So let's jump in and get started.
So the problem we've got at the moment is that our car is going a fixed amount of speed and a fixed
amount of rotation.
What if we want to have pushed down the accelerator and go faster or slower or turn left or turn right?
We can't do that at the moment.
That's because we've got these numbers hardcoded into our method.
Instead of doing that, we want to use variables and you can think of variables like boxes.
They help us store, manipulate and refer to information.
So for example, here I've got this box, it's called hit points and it has a value of 20 stored in
it.
At the moment, each variable has to have a name.
The name here is hit points.
Each variable contains data.
Presently it's 20, it could be zero, it could be a million, and each variable is of a particular
type.
This is really important.
The type here is whole numbers, and whole numbers are known as integers.
So we would write this or declare this in our script as int.
This is the type int as in integer and then the name hit points and we use a lowercase letter first
of all like a lowercase h and then uppercase for any other words that follow.
So hit points written like this equals 20 and then semicolon we put semicolon at the end of things to
say we are done with this particular statement.
So that is how we declare a variable.
So declaring it as a variable called hit points of type int and also how we assign a value to that variable
or define a variable.
The hit points is currently equal to 20.
In terms of types, there's a few common types that we'll use INT is for whole numbers, float is for
fractional numbers up to six or seven decimal places, double is for fractional numbers up to 15 decimal
places.
To be honest, I don't really use double ever because for me six decimal places is usually plenty when
it comes to the precision I need.
But if you needed a ton more precision you might use a double bull is for a true or false variable.
So if you want to say this is true, do a thing.
Or if it's false, don't do a thing.
Balls are very handy and string is when we wish to have a sequence of characters that we print out exactly
as we've typed them in.
And strings require quotation marks in order to say This is what we would need to create.
So let's look at those examples a bit more specifically.
If we wanted to create a float called speed, then we would type in float speed equals and then whatever
the value we wanted it to be, in this case, 3.8 And as we've talked about before, if you are using
a float and you have a decimal place in here, you need to put an F at the end of it mostly.
So that unity knows, Oh, it's a float and not a double.
We put the F at the end there and then with a bull we have the name is is alive equals true.
So we could define it as true or as false.
And then, for example, with a string, we once again start off with the type string and then we would
give it a name here, give it my name and my name is Rick.
And once again, using our box analogy, the box is called name and the information within that is Rick,
which is just four characters, one after another in this particular sequence.
So let's create ourselves a variable.
Now, we need to put this in a specific spot.
We want to put it within our class.
These are all the things we're creating here within our class, but we put it above our start method,
so we're declaring some variables right at the top here I'm going to declare a variable which would
be of type float.
It's going to be a float because I need decimal places and I will call this steer speed.
I could call it steering speed or turn speed, any of those kind of things, but steer speed makes sense
for me.
Steer is what you do on the steering wheel and speed is obviously how fast we're going to turn or how
fast we're going to steer and then equals and I will initialize this or in other words, give it the
first value of the value that we've currently got down here at the moment, which is 0.1 F and then
semicolon.
Now what do we do?
Well, we've got this steer speed variable.
We pop that in here within our rotate method instead of using the actual value and type in steer speed.
And just to reiterate, the reason we're doing this and not leaving that number in there is because
we want to change steer speed based upon things that happen in our game.
So sometimes this B will be 0.1 F, sometimes it'll be zero, sometimes it will be one, for example.
So we need to be able to change this, which will do an upcoming lectures.
But if we just have a hard coded number in there, we can't change it on the fly now, can we?
Okay.
So we've added steer speed and I've got a challenge for you to make sure that you're following along
to create a variable called move speed and to use that variable in your transform translate method in
a similar way to that which we've just done in our rotate method.
So please pause a video, take on that challenge.
See you back here when you're done.
Rightio.
So we will create ourselves a new variable which will be of type float as well and we'll call this move
speed and that will equal whatever we're using down here at the moment, 0.01 F and this is just where
we're initializing it.
This can change.
This is just saying what the starting point is.
And then instead of having our numbers baked in down here, we're going to add move speed as our variable,
save that up, jump back over into unity.
Make sure this all worked as a starting point.
We'll click on play.
The behavior should be exactly the same.
Yes, it is.
Fantastic.
Okay.
And then we'll do one last thing just to make sure that all works.
Instead of having steer speed at 0.1 F, let's make it say one F.
Now, just a side note.
When you've got a float and you've got a whole number, you could just not put the F there.
And Unity will say, That's cool, I'm okay with that.
No errors in this instance.
Or you can put the F there and inside.
That's fine as well.
To be perfectly honest, sometimes I do an F, sometimes I don't do an F depending upon whether I forget
or I'm paying attention.
So it's good habit to put an F when you've got values that relate to float.
So I'm going to leave the F in there.
But sometimes you might see me accidentally not have the F just so you know, jump back over into unity,
click on play.
What should we expect to see here?
Well, we should expect to see that our thing spins around a lot more quickly.
Zoom, zoom, zoom.
There we go.
Still with the same forward but more turning per frame.
So that's the behavior we get.
In the next lecture, I'll show you a way to be able to change those values right here from within.
Unity and not having to go back to your code each time if you want to change it.
So I will see you in the next lecture.