1
00:00:03,160 --> 00:00:05,500
In this lecture, we need our character to jump.

2
00:00:05,500 --> 00:00:08,740
So we're going to implement the code for jumping and do a bit of tuning.

3
00:00:08,740 --> 00:00:14,050
Now we could add a jumping animation, but I'm okay just using the running and idling depending upon

4
00:00:14,050 --> 00:00:14,890
what the player is doing.

5
00:00:14,890 --> 00:00:18,910
I think that'll look fine if you if you jump in it still using the running animation that is.

6
00:00:18,910 --> 00:00:24,400
Okay, let's have a little bit of a reminder in our input actions information.

7
00:00:24,400 --> 00:00:26,980
We created a jump method or a jump action.

8
00:00:26,980 --> 00:00:27,550
Sorry.

9
00:00:27,550 --> 00:00:31,540
And this jump action automatically creates the on jump message.

10
00:00:31,540 --> 00:00:32,650
The on jump message.

11
00:00:32,650 --> 00:00:33,670
What's inside that?

12
00:00:33,670 --> 00:00:35,830
Well, nothing yet because we haven't created it.

13
00:00:35,830 --> 00:00:40,720
But we will create things that the game will do when on jump is called.

14
00:00:40,720 --> 00:00:41,740
And when is it called?

15
00:00:41,740 --> 00:00:45,220
Well, it's called when either of these two bindings is enacted.

16
00:00:45,220 --> 00:00:50,110
So when space is clicked or when a button south is clicked enacted.

17
00:00:50,110 --> 00:00:51,490
That's not a word I use every day.

18
00:00:51,490 --> 00:00:54,340
Anyway, we've learnt something and so here is jump.

19
00:00:54,340 --> 00:00:58,870
And as I showed you before, if you click on player and look for the player input, you can see that

20
00:00:58,870 --> 00:01:02,980
one of the messages down in here is going to be on jump just there.

21
00:01:02,980 --> 00:01:08,950
So over into our script, our player movement script, let's see underneath on move I will create void

22
00:01:08,950 --> 00:01:14,200
on jump and once again we need a particular value to be passed into us.

23
00:01:14,200 --> 00:01:15,850
We need to know whether the button's pushed or not.

24
00:01:15,850 --> 00:01:18,430
So we will create input value.

25
00:01:18,430 --> 00:01:23,950
Once again, we'll just call this value and then in our curly braces and on jump is a little bit different

26
00:01:23,950 --> 00:01:25,570
to our on move and our run.

27
00:01:25,570 --> 00:01:26,590
So on move.

28
00:01:26,590 --> 00:01:32,440
We wanted to know what's the vector two and because it doesn't change every frame it might stay on,

29
00:01:32,470 --> 00:01:35,410
say, plus one while the player is holding down a key.

30
00:01:35,410 --> 00:01:41,230
We needed a way of storing the plus one up here in our move input and then in update saying hey, continue

31
00:01:41,230 --> 00:01:43,780
to run if the circumstances are appropriate.

32
00:01:43,780 --> 00:01:48,730
In other words, it's greater or less than zero, but with jump it's a button, so we don't need to

33
00:01:48,730 --> 00:01:50,020
be putting this into update.

34
00:01:50,020 --> 00:01:53,050
It literally is when the button is pressed, do a thing.

35
00:01:53,050 --> 00:01:58,600
So we're going to create a bit of an if statement, if the value dot is pressed.

36
00:01:58,600 --> 00:01:59,830
That's our way of doing it up here.

37
00:01:59,830 --> 00:02:03,070
It was valued at get vector too, but this is valued is pressed.

38
00:02:03,070 --> 00:02:05,680
If it's pressed, then do some stuff.

39
00:02:05,860 --> 00:02:06,760
Do stuff.

40
00:02:06,760 --> 00:02:11,020
I'm giggling because I was about to write do stuff which is incredibly sophisticated programming.

41
00:02:11,020 --> 00:02:11,830
Starting Point.

42
00:02:11,830 --> 00:02:17,500
We know we're working with the rigid body and also with velocity, and this is why we want to add a

43
00:02:17,500 --> 00:02:17,980
force to it.

44
00:02:17,980 --> 00:02:19,990
We want to throw some velocity onto this.

45
00:02:19,990 --> 00:02:21,970
So I'm going to plus equals.

46
00:02:21,970 --> 00:02:26,950
So take whatever your velocity is right now for up and down and then add something to it.

47
00:02:26,950 --> 00:02:27,940
And what will we add to it?

48
00:02:27,940 --> 00:02:32,350
Well, we'll create a new vector to once again, we need an X and a Y on the X.

49
00:02:32,350 --> 00:02:33,880
We don't want to be adding anything.

50
00:02:33,880 --> 00:02:36,070
So I think we can just say zero on there.

51
00:02:36,070 --> 00:02:39,430
Don't add a new thing with run we were assigning.

52
00:02:39,430 --> 00:02:44,260
So the velocity is now this particular velocity, but within the jump we're adding.

53
00:02:44,260 --> 00:02:47,680
So we're saying take what you've got and add some more to it.

54
00:02:47,680 --> 00:02:52,180
We don't want to add any more to the X, so we'll leave that at zero and then to the Y we want to add

55
00:02:52,180 --> 00:02:52,990
a certain amount.

56
00:02:52,990 --> 00:02:54,850
So I don't know what that certain amount is yet.

57
00:02:54,850 --> 00:03:00,220
So we'll call it jump speed as a new variable semicolon and we'll jump up to pardon the pun, jump up

58
00:03:00,220 --> 00:03:06,100
to the top serialised field and we'll make this a float and jump speed.

59
00:03:06,100 --> 00:03:10,720
Initialize this at five, maybe five f semicolon back down here.

60
00:03:10,720 --> 00:03:12,070
So jump speed should be cool.

61
00:03:12,070 --> 00:03:14,590
Now let's save that and see if it actually works.

62
00:03:14,620 --> 00:03:17,530
Click on play jump is assigned to spacebar.

63
00:03:18,280 --> 00:03:19,240
Bu there we go.

64
00:03:19,240 --> 00:03:20,140
We are jumping.

65
00:03:20,140 --> 00:03:20,770
Whoa.

66
00:03:20,770 --> 00:03:24,850
And that is some serious distance we get with our jump just there because we're floating in the air

67
00:03:24,850 --> 00:03:25,690
for quite some time.

68
00:03:25,690 --> 00:03:26,440
So that's a win.

69
00:03:26,440 --> 00:03:28,300
Let's have a look at some of the issues in here.

70
00:03:28,300 --> 00:03:32,590
If I was to push the spacebar twice, once, twice, three, four, five.

71
00:03:32,590 --> 00:03:35,380
Okay, so that's an issue we'll need to address in upcoming lectures.

72
00:03:35,380 --> 00:03:36,190
What else can we do?

73
00:03:36,190 --> 00:03:36,940
Well, we're all right.

74
00:03:36,940 --> 00:03:37,900
We're going a really long way.

75
00:03:37,900 --> 00:03:40,060
That might be what you want, but it's not really what I want.

76
00:03:40,060 --> 00:03:43,090
And if we fall down here, could we actually get out of there?

77
00:03:43,090 --> 00:03:43,780
No, we couldn't.

78
00:03:43,780 --> 00:03:46,210
So there's some things we need to tune for our character.

79
00:03:46,210 --> 00:03:49,630
Let me show you show you a couple of things, and then you can go ahead and tune this yourself.

80
00:03:49,630 --> 00:03:55,300
The first thing, if we go up to edit project settings and look for physics, 2D within here we've got

81
00:03:55,300 --> 00:03:58,990
a gravity setting so we can make the gravity heavier for the player.

82
00:03:58,990 --> 00:03:59,890
We can make it lighter.

83
00:03:59,890 --> 00:04:01,570
That's one place we could change it.

84
00:04:01,570 --> 00:04:04,210
And this will change everything in our game world.

85
00:04:04,210 --> 00:04:05,230
It'll change things.

86
00:04:05,230 --> 00:04:06,910
We shoot, it'll change enemies.

87
00:04:06,910 --> 00:04:09,490
If we change our gravity here, that's a holistic gravity.

88
00:04:09,490 --> 00:04:13,330
Or we could click on our player and then find within our rigid body.

89
00:04:13,330 --> 00:04:15,490
You can see the gravity scale for the player just here.

90
00:04:15,490 --> 00:04:18,670
And if we wanted to make the world heavier or lighter, we could change it in here.

91
00:04:18,670 --> 00:04:22,660
And so if you were changing the gravity for everything in your world, then you'd certainly want to

92
00:04:22,660 --> 00:04:26,080
go into the project settings and the physics 2D in there.

93
00:04:26,080 --> 00:04:27,310
So it changes for everything.

94
00:04:27,310 --> 00:04:30,970
But I'm pretty much only going to care about gravity for my player, so I'm just going to do it directly

95
00:04:30,970 --> 00:04:35,050
in here, make the gravity twice as strong, and then see how that goes.

96
00:04:35,050 --> 00:04:37,540
So gravity scale of two, okay, that's pretty good.

97
00:04:37,540 --> 00:04:41,380
But now he's not jumping anywhere near high enough and that's going to be changed within the player

98
00:04:41,380 --> 00:04:42,040
movement script.

99
00:04:42,040 --> 00:04:43,810
And you can see our jump speed is five in here.

100
00:04:43,810 --> 00:04:45,160
So it's time for a challenge I think.

101
00:04:45,160 --> 00:04:49,090
Tune your jump using the gravity and jump speed tune your jump so it feels good.

102
00:04:49,090 --> 00:04:52,210
Decide how many tiles you want your player to be able to jump up.

103
00:04:52,210 --> 00:04:56,440
So if I run my player at the moment down to this little bit where I've got the jump, ignore the water

104
00:04:56,440 --> 00:04:58,420
for now, it'll do some watery stuff later on.

105
00:04:58,420 --> 00:05:01,390
But down here I can only jump maybe one tile.

106
00:05:01,390 --> 00:05:02,500
I can't even get out of this.

107
00:05:02,650 --> 00:05:03,720
This is two tiles in here.

108
00:05:03,730 --> 00:05:04,530
One, two.

109
00:05:04,540 --> 00:05:08,260
I think I want my character to be able to jump a little bit over three tiles.

110
00:05:08,260 --> 00:05:12,910
So that's the tuning I'm going to be aiming for and then decide how far you're okay with your player

111
00:05:12,910 --> 00:05:14,770
floating horizontally when jumping.

112
00:05:14,770 --> 00:05:18,100
So all these things are combined to say, Well, what should I tune it?

113
00:05:18,100 --> 00:05:21,640
To tune it based upon the performance you want your player to have.

114
00:05:21,670 --> 00:05:22,930
So there's a challenge to take that on.

115
00:05:22,960 --> 00:05:24,040
C back here when you're done.

116
00:05:26,590 --> 00:05:26,970
Okay.

117
00:05:26,980 --> 00:05:28,960
First thing, I need my character to jump higher.

118
00:05:28,960 --> 00:05:30,750
So Jump Speed will put this to 15.

119
00:05:30,760 --> 00:05:33,730
Click on play, see what happens, but jump down here.

120
00:05:34,620 --> 00:05:36,340
But that's now far too fast.

121
00:05:36,340 --> 00:05:39,430
So they make the gravity heavier or the jump speed less.

122
00:05:39,520 --> 00:05:42,550
I think I'm going to try the gravity heavier while I'm still in plane mode.

123
00:05:42,550 --> 00:05:45,130
I'll do this so I can change these things on the fly.

124
00:05:45,160 --> 00:05:46,150
That's pretty good.

125
00:05:46,150 --> 00:05:47,380
It's floaty.

126
00:05:47,380 --> 00:05:49,330
It gets quite a long distance, though, doesn't he?

127
00:05:49,330 --> 00:05:50,980
So jump speed will put to 20.

128
00:05:50,980 --> 00:05:53,740
Gravity will plop up to four.

129
00:05:53,740 --> 00:05:55,360
See how that impacts blow?

130
00:05:55,360 --> 00:05:56,440
He's flying way too far.

131
00:05:56,470 --> 00:05:58,540
Gravity will put to ten just to be crazy.

132
00:06:00,550 --> 00:06:00,760
Okay.

133
00:06:00,880 --> 00:06:04,420
And there's a good issue that we've spotted just there that he fell right through the world.

134
00:06:04,420 --> 00:06:05,770
So I'll get back to that in a moment.

135
00:06:05,770 --> 00:06:07,810
Just mental note, I think maybe eight.

136
00:06:07,810 --> 00:06:10,930
So eight on the gravity scale and I click out of play mode.

137
00:06:10,930 --> 00:06:16,000
It's going to go back to what it was eight and 20, I think I had here jump speed.

138
00:06:16,210 --> 00:06:19,240
We'll see how that goes and I'll play around with that a little bit more.

139
00:06:19,240 --> 00:06:21,370
But we just want near enough is good enough for the moment.

140
00:06:21,370 --> 00:06:26,440
The other thing was with the player missing the collision, that's because we currently have our collision

141
00:06:26,440 --> 00:06:27,640
detection on discrete.

142
00:06:27,640 --> 00:06:29,140
We want to change that to continuous.

143
00:06:29,140 --> 00:06:32,050
Continuous is better for things that are moving a lot and moving quickly.

144
00:06:32,050 --> 00:06:34,450
It's a little bit more expensive computationally.

145
00:06:34,450 --> 00:06:38,290
So we only want to do that really for the player character and anything really important.

146
00:06:38,290 --> 00:06:39,640
Pop that on to continuous.

147
00:06:39,670 --> 00:06:41,260
We'll see if we still have that problem.

148
00:06:42,950 --> 00:06:43,160
All right.

149
00:06:43,300 --> 00:06:44,050
This was the spot.

150
00:06:44,050 --> 00:06:44,920
Looks pretty good.

151
00:06:44,920 --> 00:06:45,730
I like the speed.

152
00:06:45,730 --> 00:06:47,650
Now the player can get a reasonable distance.

153
00:06:47,650 --> 00:06:48,820
It feels pretty good, actually.

154
00:06:48,820 --> 00:06:52,360
The only test I've got now is can he reach the third level?

155
00:06:52,360 --> 00:06:55,390
So I just need to make the jump speed a little bit more than I think I'm done.

156
00:06:55,390 --> 00:06:58,030
So 25 but I'm going to put it somewhere around 25.

157
00:06:58,060 --> 00:07:01,000
My gravity I think was somewhere around four, wasn't it?

158
00:07:01,000 --> 00:07:02,440
So there we go.

159
00:07:02,470 --> 00:07:03,880
That's feeling pretty good.

160
00:07:03,970 --> 00:07:05,440
Like Bing's like a little jumping.

161
00:07:05,440 --> 00:07:09,820
Jack I kind of like the feeling, but you can have yours tuned however you like that works for you.

162
00:07:09,850 --> 00:07:12,880
Mine's at Gravity Scale eight and Jump Speed 25.

163
00:07:12,880 --> 00:07:13,390
So there we go.

164
00:07:13,390 --> 00:07:16,360
We've implemented basic jumping into our game.

165
00:07:16,360 --> 00:07:19,720
There's a few more things that we need to resolve with jumping, couple of bugs, but we've got the

166
00:07:19,720 --> 00:07:20,590
basics for it.

167
00:07:20,590 --> 00:07:21,970
Okay, so in the next lecture.

