1
00:00:03,040 --> 00:00:03,580
Hello again.

2
00:00:03,580 --> 00:00:07,990
In this lecture, we're going to be adding some variables so that we can play around with our numbers

3
00:00:07,990 --> 00:00:11,920
more easily and make our object change its behavior.

4
00:00:11,920 --> 00:00:13,480
So let's jump in and get started.

5
00:00:13,480 --> 00:00:17,980
So the problem we've got at the moment is that our car is going a fixed amount of speed and a fixed

6
00:00:17,980 --> 00:00:18,850
amount of rotation.

7
00:00:18,850 --> 00:00:23,860
What if we want to have pushed down the accelerator and go faster or slower or turn left or turn right?

8
00:00:23,860 --> 00:00:25,270
We can't do that at the moment.

9
00:00:25,270 --> 00:00:29,740
That's because we've got these numbers hardcoded into our method.

10
00:00:29,740 --> 00:00:35,140
Instead of doing that, we want to use variables and you can think of variables like boxes.

11
00:00:35,140 --> 00:00:38,980
They help us store, manipulate and refer to information.

12
00:00:38,980 --> 00:00:45,100
So for example, here I've got this box, it's called hit points and it has a value of 20 stored in

13
00:00:45,100 --> 00:00:45,220
it.

14
00:00:45,220 --> 00:00:47,920
At the moment, each variable has to have a name.

15
00:00:47,920 --> 00:00:49,510
The name here is hit points.

16
00:00:49,510 --> 00:00:51,490
Each variable contains data.

17
00:00:51,490 --> 00:00:56,620
Presently it's 20, it could be zero, it could be a million, and each variable is of a particular

18
00:00:56,620 --> 00:00:57,250
type.

19
00:00:57,250 --> 00:00:58,480
This is really important.

20
00:00:58,480 --> 00:01:02,320
The type here is whole numbers, and whole numbers are known as integers.

21
00:01:02,320 --> 00:01:06,970
So we would write this or declare this in our script as int.

22
00:01:06,970 --> 00:01:13,720
This is the type int as in integer and then the name hit points and we use a lowercase letter first

23
00:01:13,720 --> 00:01:17,770
of all like a lowercase h and then uppercase for any other words that follow.

24
00:01:17,770 --> 00:01:23,380
So hit points written like this equals 20 and then semicolon we put semicolon at the end of things to

25
00:01:23,380 --> 00:01:25,450
say we are done with this particular statement.

26
00:01:25,450 --> 00:01:28,510
So that is how we declare a variable.

27
00:01:28,510 --> 00:01:35,800
So declaring it as a variable called hit points of type int and also how we assign a value to that variable

28
00:01:35,800 --> 00:01:37,150
or define a variable.

29
00:01:37,180 --> 00:01:39,880
The hit points is currently equal to 20.

30
00:01:39,880 --> 00:01:45,640
In terms of types, there's a few common types that we'll use INT is for whole numbers, float is for

31
00:01:45,640 --> 00:01:51,940
fractional numbers up to six or seven decimal places, double is for fractional numbers up to 15 decimal

32
00:01:51,940 --> 00:01:52,450
places.

33
00:01:52,450 --> 00:01:58,420
To be honest, I don't really use double ever because for me six decimal places is usually plenty when

34
00:01:58,420 --> 00:02:00,280
it comes to the precision I need.

35
00:02:00,280 --> 00:02:06,550
But if you needed a ton more precision you might use a double bull is for a true or false variable.

36
00:02:06,550 --> 00:02:08,560
So if you want to say this is true, do a thing.

37
00:02:08,560 --> 00:02:09,850
Or if it's false, don't do a thing.

38
00:02:09,850 --> 00:02:15,790
Balls are very handy and string is when we wish to have a sequence of characters that we print out exactly

39
00:02:15,790 --> 00:02:16,930
as we've typed them in.

40
00:02:16,930 --> 00:02:22,510
And strings require quotation marks in order to say This is what we would need to create.

41
00:02:22,510 --> 00:02:24,820
So let's look at those examples a bit more specifically.

42
00:02:24,820 --> 00:02:31,750
If we wanted to create a float called speed, then we would type in float speed equals and then whatever

43
00:02:31,750 --> 00:02:37,360
the value we wanted it to be, in this case, 3.8 And as we've talked about before, if you are using

44
00:02:37,360 --> 00:02:42,070
a float and you have a decimal place in here, you need to put an F at the end of it mostly.

45
00:02:42,070 --> 00:02:45,340
So that unity knows, Oh, it's a float and not a double.

46
00:02:45,340 --> 00:02:50,920
We put the F at the end there and then with a bull we have the name is is alive equals true.

47
00:02:50,920 --> 00:02:53,620
So we could define it as true or as false.

48
00:02:53,620 --> 00:02:58,630
And then, for example, with a string, we once again start off with the type string and then we would

49
00:02:58,630 --> 00:03:01,240
give it a name here, give it my name and my name is Rick.

50
00:03:01,240 --> 00:03:06,820
And once again, using our box analogy, the box is called name and the information within that is Rick,

51
00:03:06,820 --> 00:03:11,020
which is just four characters, one after another in this particular sequence.

52
00:03:11,020 --> 00:03:13,090
So let's create ourselves a variable.

53
00:03:13,090 --> 00:03:15,100
Now, we need to put this in a specific spot.

54
00:03:15,100 --> 00:03:17,470
We want to put it within our class.

55
00:03:17,470 --> 00:03:22,270
These are all the things we're creating here within our class, but we put it above our start method,

56
00:03:22,270 --> 00:03:26,620
so we're declaring some variables right at the top here I'm going to declare a variable which would

57
00:03:26,620 --> 00:03:28,120
be of type float.

58
00:03:28,150 --> 00:03:34,780
It's going to be a float because I need decimal places and I will call this steer speed.

59
00:03:34,780 --> 00:03:39,730
I could call it steering speed or turn speed, any of those kind of things, but steer speed makes sense

60
00:03:39,730 --> 00:03:40,120
for me.

61
00:03:40,120 --> 00:03:45,910
Steer is what you do on the steering wheel and speed is obviously how fast we're going to turn or how

62
00:03:45,910 --> 00:03:50,890
fast we're going to steer and then equals and I will initialize this or in other words, give it the

63
00:03:50,890 --> 00:03:57,460
first value of the value that we've currently got down here at the moment, which is 0.1 F and then

64
00:03:57,460 --> 00:03:58,270
semicolon.

65
00:03:58,270 --> 00:03:59,080
Now what do we do?

66
00:03:59,080 --> 00:04:01,270
Well, we've got this steer speed variable.

67
00:04:01,270 --> 00:04:08,380
We pop that in here within our rotate method instead of using the actual value and type in steer speed.

68
00:04:08,380 --> 00:04:11,980
And just to reiterate, the reason we're doing this and not leaving that number in there is because

69
00:04:11,980 --> 00:04:16,000
we want to change steer speed based upon things that happen in our game.

70
00:04:16,000 --> 00:04:21,399
So sometimes this B will be 0.1 F, sometimes it'll be zero, sometimes it will be one, for example.

71
00:04:21,399 --> 00:04:24,610
So we need to be able to change this, which will do an upcoming lectures.

72
00:04:24,610 --> 00:04:28,660
But if we just have a hard coded number in there, we can't change it on the fly now, can we?

73
00:04:28,810 --> 00:04:29,180
Okay.

74
00:04:29,200 --> 00:04:33,610
So we've added steer speed and I've got a challenge for you to make sure that you're following along

75
00:04:33,610 --> 00:04:39,460
to create a variable called move speed and to use that variable in your transform translate method in

76
00:04:39,460 --> 00:04:42,070
a similar way to that which we've just done in our rotate method.

77
00:04:42,070 --> 00:04:44,470
So please pause a video, take on that challenge.

78
00:04:44,470 --> 00:04:45,790
See you back here when you're done.

79
00:04:48,490 --> 00:04:49,360
Rightio.

80
00:04:49,360 --> 00:04:54,760
So we will create ourselves a new variable which will be of type float as well and we'll call this move

81
00:04:54,760 --> 00:05:02,710
speed and that will equal whatever we're using down here at the moment, 0.01 F and this is just where

82
00:05:02,710 --> 00:05:04,210
we're initializing it.

83
00:05:04,210 --> 00:05:05,200
This can change.

84
00:05:05,200 --> 00:05:07,420
This is just saying what the starting point is.

85
00:05:07,420 --> 00:05:13,660
And then instead of having our numbers baked in down here, we're going to add move speed as our variable,

86
00:05:13,660 --> 00:05:15,850
save that up, jump back over into unity.

87
00:05:15,850 --> 00:05:18,010
Make sure this all worked as a starting point.

88
00:05:18,010 --> 00:05:19,450
We'll click on play.

89
00:05:19,480 --> 00:05:21,760
The behavior should be exactly the same.

90
00:05:21,760 --> 00:05:22,540
Yes, it is.

91
00:05:22,540 --> 00:05:23,530
Fantastic.

92
00:05:23,530 --> 00:05:24,070
Okay.

93
00:05:24,070 --> 00:05:26,830
And then we'll do one last thing just to make sure that all works.

94
00:05:26,830 --> 00:05:31,840
Instead of having steer speed at 0.1 F, let's make it say one F.

95
00:05:31,840 --> 00:05:33,130
Now, just a side note.

96
00:05:33,130 --> 00:05:37,840
When you've got a float and you've got a whole number, you could just not put the F there.

97
00:05:37,840 --> 00:05:40,240
And Unity will say, That's cool, I'm okay with that.

98
00:05:40,240 --> 00:05:41,920
No errors in this instance.

99
00:05:41,920 --> 00:05:43,360
Or you can put the F there and inside.

100
00:05:43,360 --> 00:05:44,290
That's fine as well.

101
00:05:44,290 --> 00:05:48,580
To be perfectly honest, sometimes I do an F, sometimes I don't do an F depending upon whether I forget

102
00:05:48,580 --> 00:05:49,660
or I'm paying attention.

103
00:05:49,660 --> 00:05:53,620
So it's good habit to put an F when you've got values that relate to float.

104
00:05:53,620 --> 00:05:54,970
So I'm going to leave the F in there.

105
00:05:54,970 --> 00:06:00,700
But sometimes you might see me accidentally not have the F just so you know, jump back over into unity,

106
00:06:00,700 --> 00:06:01,330
click on play.

107
00:06:01,360 --> 00:06:02,860
What should we expect to see here?

108
00:06:02,860 --> 00:06:06,520
Well, we should expect to see that our thing spins around a lot more quickly.

109
00:06:06,550 --> 00:06:07,350
Zoom, zoom, zoom.

110
00:06:07,360 --> 00:06:08,020
There we go.

111
00:06:08,020 --> 00:06:11,500
Still with the same forward but more turning per frame.

112
00:06:11,500 --> 00:06:13,150
So that's the behavior we get.

113
00:06:13,150 --> 00:06:17,170
In the next lecture, I'll show you a way to be able to change those values right here from within.

114
00:06:17,170 --> 00:06:21,100
Unity and not having to go back to your code each time if you want to change it.

115
00:06:21,100 --> 00:06:22,630
So I will see you in the next lecture.

