1
00:00:03,020 --> 00:00:08,029
In this lecture, we're going to be making our play a drop from the sky and run left and run right when

2
00:00:08,029 --> 00:00:09,920
we push on the keys on the keyboard.

3
00:00:09,920 --> 00:00:11,060
So let's get started.

4
00:00:11,100 --> 00:00:15,980
First question I have for you here is what do we need to access on our player to make the player move

5
00:00:15,980 --> 00:00:16,370
around?

6
00:00:16,370 --> 00:00:21,680
What's the core movement thing that physics applies to or allows us to push the player around?

7
00:00:21,680 --> 00:00:24,290
It is the rigid body.

8
00:00:24,290 --> 00:00:26,870
So well done if you guess that or yelled it at your computer out loud.

9
00:00:26,870 --> 00:00:28,400
So we need to access the rigid body.

10
00:00:28,400 --> 00:00:32,780
Let's start in our script by caching a reference to rigid body.

11
00:00:32,780 --> 00:00:35,570
So if you know how to do that, pause a video and do that now.

12
00:00:36,990 --> 00:00:38,940
And if you're thinking, What's Rick talking about?

13
00:00:38,940 --> 00:00:40,070
Let's do that together.

14
00:00:40,080 --> 00:00:45,440
So I need to create a variable which would be a type rigid body 2D.

15
00:00:45,480 --> 00:00:46,400
Don't forget the 2D.

16
00:00:46,410 --> 00:00:51,300
I often forget that also there's not a capitalization on the B, which I mess up as well at times,

17
00:00:51,300 --> 00:00:58,140
and we'll name this rigid body actually rename this my rigid body so that it doesn't complain.

18
00:00:58,140 --> 00:01:02,580
Sometimes it doesn't like when you just do rigid body because it thinks you're referring to something

19
00:01:02,580 --> 00:01:02,700
else.

20
00:01:02,700 --> 00:01:07,020
So my rigid body and semicolon now within start, what do we need to do?

21
00:01:07,050 --> 00:01:11,780
We need to my rigid body equals get component.

22
00:01:11,790 --> 00:01:13,050
What sort of component is it?

23
00:01:13,050 --> 00:01:18,960
It's a rigid body to dx parentheses semicolon.

24
00:01:18,960 --> 00:01:20,100
Okay, now we've got to read your body.

25
00:01:20,130 --> 00:01:20,910
What do we need to do?

26
00:01:20,910 --> 00:01:23,220
Well, we need to create a run method.

27
00:01:23,220 --> 00:01:27,420
We need to say, move this way, move that way, run based upon what the player input is.

28
00:01:27,420 --> 00:01:30,450
So first of all, in update, I'm going to call the method run.

29
00:01:30,450 --> 00:01:32,340
I haven't created this yet, but I'll say run.

30
00:01:32,340 --> 00:01:33,360
What does run mean?

31
00:01:33,360 --> 00:01:36,690
Well, let's jump down right below on move.

32
00:01:36,690 --> 00:01:41,880
We will create ourselves a new method void run parentheses, curly braces.

33
00:01:41,880 --> 00:01:46,710
So let's start with a really simple implementation and then we'll make it a little bit cleverer.

34
00:01:46,710 --> 00:01:50,880
So within here, what we're trying to do is say my rigid body velocity.

35
00:01:50,880 --> 00:01:54,660
That's how we're going to be moving around our character equals what?

36
00:01:54,660 --> 00:01:58,140
Well, we can just say for now equals our move input.

37
00:01:58,140 --> 00:02:05,790
That's our vector to move input per semicolon, save that up and then jump back into unity.

38
00:02:05,790 --> 00:02:06,780
Click on play.

39
00:02:06,810 --> 00:02:07,890
What happens in here?

40
00:02:08,070 --> 00:02:10,620
Well, we can go left, right, up and down.

41
00:02:10,620 --> 00:02:11,190
Excellent.

42
00:02:11,190 --> 00:02:12,120
But it's kind of weird.

43
00:02:12,120 --> 00:02:16,800
The play is floating, which we don't want, and the play is moving kind of slow, which we don't want.

44
00:02:16,800 --> 00:02:19,020
And the play is actually being out of fly up and down.

45
00:02:19,020 --> 00:02:21,810
We only want the player to be able to move up and down when they're on a ladder.

46
00:02:21,840 --> 00:02:25,380
Let's tidy up our run method in here to be a little bit more sophisticated.

47
00:02:25,380 --> 00:02:30,720
So rather than just straight up move input, let's create ourselves a new variable which would be of

48
00:02:30,720 --> 00:02:37,170
type vector to and we'll call this player velocity and that equals and we know we can't just say that

49
00:02:37,170 --> 00:02:40,560
equals one and three because unity will say, I don't know what you're talking about here, one and

50
00:02:40,560 --> 00:02:40,800
three.

51
00:02:40,800 --> 00:02:41,430
What does that mean?

52
00:02:41,430 --> 00:02:45,240
So we need to say, well, I'm going to give you two values and it will be in a vector two.

53
00:02:45,240 --> 00:02:51,390
So new vector two, we cast it to a vector two and then player velocity knows what it is that it's supposed

54
00:02:51,390 --> 00:02:56,910
to be receiving, what these numbers are that it's getting and putting into its vector two values.

55
00:02:56,910 --> 00:03:00,900
So what I'm going to do on the x axis is move input.

56
00:03:00,900 --> 00:03:02,880
That's currently a vector to remember.

57
00:03:02,880 --> 00:03:08,970
I only want this to be on the x, so let's move input x to say just give me the x value and then comma.

58
00:03:09,000 --> 00:03:13,230
I mean we could put move input y, we would get exactly the same behavior as we just had.

59
00:03:13,230 --> 00:03:14,100
So we don't want that.

60
00:03:14,100 --> 00:03:15,330
We want something different in here.

61
00:03:15,330 --> 00:03:19,050
We don't want the player to be able to push up and down unless they're on a ladder.

62
00:03:19,050 --> 00:03:22,050
And that will be in a different method, which we'll call climb ladder.

63
00:03:22,050 --> 00:03:23,580
We don't want that in run.

64
00:03:23,580 --> 00:03:26,310
So for now, I'm just going to put zero and see what happens.

65
00:03:26,310 --> 00:03:31,290
Oh, and then the last step here is we need to make sure instead of move input, we need to have player

66
00:03:31,290 --> 00:03:38,580
velocity as the values that our rigid body velocity is receiving.

67
00:03:38,580 --> 00:03:42,330
So player velocity, save that up, back over into unity.

68
00:03:42,330 --> 00:03:43,260
Click play.

69
00:03:43,290 --> 00:03:45,120
Now we can't move up and down.

70
00:03:45,120 --> 00:03:45,720
That's great.

71
00:03:45,720 --> 00:03:46,230
Perfect.

72
00:03:46,230 --> 00:03:48,750
But I can move left and I can move right now.

73
00:03:48,750 --> 00:03:49,920
We've got some weird behavior.

74
00:03:49,920 --> 00:03:54,630
The players are slowly floating down because we're saying zero, don't give it any velocity.

75
00:03:54,660 --> 00:03:57,690
It's trying to resist gravity, which is not what we want.

76
00:03:57,690 --> 00:04:00,180
So let's go back to our code.

77
00:04:00,180 --> 00:04:05,280
I've got a challenge for you in here, and one is a simple challenge and one is a tricky challenge if

78
00:04:05,280 --> 00:04:05,910
you're ready for it.

79
00:04:05,910 --> 00:04:11,370
So the simple challenge here is add something to our script to give us control over how fast the player

80
00:04:11,370 --> 00:04:11,820
runs.

81
00:04:11,820 --> 00:04:15,630
Because you can see it's not a very compelling platform if you're only moving that quickly.

82
00:04:15,630 --> 00:04:16,920
So that's the simple one.

83
00:04:16,920 --> 00:04:23,160
And then the tricky one is trying to figure out how to make the character behave normally on the Y axis.

84
00:04:23,160 --> 00:04:26,730
In other words, to respond to gravity, to not be able to fly up and down the air.

85
00:04:26,730 --> 00:04:29,370
We've already removed that and to not get stuck.

86
00:04:29,370 --> 00:04:32,490
So not just to be sort of stuck there floating in the middle of nowhere.

87
00:04:32,490 --> 00:04:33,810
So see if you can have it.

88
00:04:33,810 --> 00:04:38,580
So if you can figure out to say just behave normally, it's a tricky one, this one.

89
00:04:38,580 --> 00:04:39,690
So if you don't get it, no worries.

90
00:04:39,690 --> 00:04:40,350
But give it a shot.

91
00:04:40,350 --> 00:04:43,830
Have a think about it, see if you can wrestle with it and find the solution.

92
00:04:43,830 --> 00:04:44,850
So pause the video.

93
00:04:44,850 --> 00:04:47,010
Take that on C back here when you are done.

94
00:04:50,770 --> 00:04:58,240
First step, I'm going to add a new serialized field right up the very top serialize field, and this

95
00:04:58,240 --> 00:05:04,960
will be of Type Float and I'll call this run speed and I'll initialize that at say ten, ten F because

96
00:05:04,960 --> 00:05:09,880
it's a float and that's going to be our variable that we will then jump back down here and run and say,

97
00:05:09,880 --> 00:05:15,970
move input X multiplied by run speed so that we can speed up how fast we're moving.

98
00:05:15,970 --> 00:05:19,810
If we're receiving a minus one to plus one, we can then multiply it by ten.

99
00:05:19,810 --> 00:05:25,420
So it'll be from minus ten to plus ten that we're adding to the player velocity or pushing the player

100
00:05:25,420 --> 00:05:26,350
each frame.

101
00:05:26,350 --> 00:05:29,800
Now what are we going to do about this pesky y value in our vector two?

102
00:05:29,830 --> 00:05:37,480
What I'm going to do here is access the my rigid body velocity dot y and say that's it.

103
00:05:37,480 --> 00:05:43,180
And what my logic here is, is saying that whatever your current velocity is on y, just keep that,

104
00:05:43,180 --> 00:05:47,320
just do the same because we're adding a new velocity each frame.

105
00:05:47,320 --> 00:05:50,350
We only need to be adding the x, we don't want to be touching the Y.

106
00:05:50,350 --> 00:05:54,580
So instead of saying it's zero, we say just keep it as it currently is.

107
00:05:54,580 --> 00:05:58,150
So let's see before I celebrate too much, I'll save that and see if that worked.

108
00:05:58,300 --> 00:05:59,590
Click on a play.

109
00:06:00,510 --> 00:06:02,280
But drops to the ground and left, right.

110
00:06:02,280 --> 00:06:06,180
And we're running really fast, probably a little bit too fast, but we can tune that later on.

111
00:06:06,180 --> 00:06:06,960
We can't move up.

112
00:06:06,960 --> 00:06:07,920
We can't move down.

113
00:06:07,920 --> 00:06:10,740
But the player is responding to gravity.

114
00:06:10,980 --> 00:06:12,390
There we go, falling off the screen.

115
00:06:12,390 --> 00:06:13,140
So there we go.

116
00:06:13,170 --> 00:06:18,090
We've implemented left and right movement for our player using this code, using our run method so that

117
00:06:18,090 --> 00:06:20,070
we can move left and right with our player.

118
00:06:20,070 --> 00:06:21,060
I'll see in the next lecture.

