1
00:00:00,000 --> 00:00:01,000
Hello, boys and girls.

2
00:00:01,000 --> 00:00:06,000
And in this video, we're going to check collision and surroundings around the player.

3
00:00:06,000 --> 00:00:14,000
So at the moment, you can see if I start my game, I can jump and I can jump more and more and more

4
00:00:14,000 --> 00:00:15,000
and more and more.

5
00:00:17,000 --> 00:00:18,000
And then basically I can fly.

6
00:00:18,000 --> 00:00:24,000
But we want to be able to jump only if player is on the ground, how we do that.

7
00:00:24,000 --> 00:00:28,000
We just need to make sure player is on the ground.

8
00:00:28,000 --> 00:00:33,000
And for that, we're going to go to the player script and create something here.

9
00:00:33,000 --> 00:00:36,000
We're going to create a ground check.

10
00:00:37,000 --> 00:00:41,000
Let's go somewhere below and type private.

11
00:00:41,000 --> 00:00:46,000
Ball is grounded like that.

12
00:00:47,000 --> 00:00:48,000
Now we have that.

13
00:00:48,000 --> 00:00:57,000
Let's go to update and type is grounded equals two x to DD dot recast.

14
00:00:57,000 --> 00:01:04,000
And now I have my internal suggestion here, but it's not very correct if I put my mouse on the recast

15
00:01:04,000 --> 00:01:05,000
or I can see what I need.

16
00:01:05,000 --> 00:01:12,000
So first I need origin from where I'm shooting my recast and for that I'm going to type transform the

17
00:01:12,000 --> 00:01:13,000
position.

18
00:01:13,000 --> 00:01:17,000
Transform position is the position of the player exact position he has.

19
00:01:17,000 --> 00:01:20,000
So it will be basically shot from the middle of the player.

20
00:01:20,000 --> 00:01:25,000
Then we need direction and because we want to check what's below the feet of the player, we're going

21
00:01:25,000 --> 00:01:28,000
to do vector to dot down.

22
00:01:28,000 --> 00:01:30,000
Then we need distance and we did not create that.

23
00:01:30,000 --> 00:01:37,000
So let's go over here and create public float ground, check distance.

24
00:01:38,000 --> 00:01:39,000
Very cool.

25
00:01:39,000 --> 00:01:41,000
Now we put it here.

26
00:01:41,000 --> 00:01:42,000
Ground check, distance.

27
00:01:44,000 --> 00:01:50,000
And we can see we need a layer mask because if we don't use layer mask recast, going to detect everything

28
00:01:50,000 --> 00:01:51,000
he finds, including the player.

29
00:01:51,000 --> 00:01:56,000
So let's go over here and create public layer mask.

30
00:01:56,000 --> 00:01:59,000
What is ground?

31
00:01:59,000 --> 00:02:03,000
Now we type here, what is ground and now we have boolean.

32
00:02:03,000 --> 00:02:11,000
That is true only if recast from position of the player that goes down in a distance of ground.

33
00:02:11,000 --> 00:02:13,000
Check in the layer mask.

34
00:02:13,000 --> 00:02:16,000
What is ground detects something in there.

35
00:02:16,000 --> 00:02:23,000
Then we go over here to jump button and we can type and is grounded.

36
00:02:25,000 --> 00:02:27,000
Now let's go back to Unity.

37
00:02:27,000 --> 00:02:33,000
First, we need to go to layer and layer and add layer ground.

38
00:02:33,000 --> 00:02:36,000
Now let's select the platform and choose layer round.

39
00:02:37,000 --> 00:02:38,000
Cool.

40
00:02:38,000 --> 00:02:40,000
Now we can go to player itself.

41
00:02:40,000 --> 00:02:44,000
Find what is ground and click ground ground shake distance.

42
00:02:44,000 --> 00:02:47,000
Let me try to put two.

43
00:02:48,000 --> 00:02:50,000
So it works.

44
00:02:50,000 --> 00:02:57,000
I can jump only if I'm on the ground, but it's not very accurate because I cannot see how long is ground

45
00:02:57,000 --> 00:02:57,000
distance.

46
00:02:57,000 --> 00:02:59,000
I need to see it.

47
00:02:59,000 --> 00:03:03,000
For that we're going to create another function back in the code.

48
00:03:03,000 --> 00:03:12,000
Let's just type on draw gizmos and inside we're going to do gizmos dot draw line and we say from where

49
00:03:12,000 --> 00:03:19,000
to where from transform position to and is going to be a bit tricky new vector two on the x it should

50
00:03:19,000 --> 00:03:20,000
be the same.

51
00:03:20,000 --> 00:03:29,000
So transform position and on the Y it should be transformed position minus ground distance weight sorry,

52
00:03:29,000 --> 00:03:34,000
transform position dot x and here transform position dot y.

53
00:03:34,000 --> 00:03:39,000
Now let's go back to Unity and over here we can see our line gizmos.

54
00:03:39,000 --> 00:03:48,000
Now, if I change my ground check distance, it will change its length so I can fix it as I like.

55
00:03:48,000 --> 00:03:50,000
And I want to make something like.

56
00:03:52,000 --> 00:03:53,000
This.

57
00:03:55,000 --> 00:03:55,000
This is.

58
00:03:55,000 --> 00:03:58,000
Okay, so maybe 1.4.

59
00:03:59,000 --> 00:04:01,000
I'm sure this will work properly.

60
00:04:06,000 --> 00:04:06,000
It does.

61
00:04:06,000 --> 00:04:08,000
Now, I don't like my jump.

62
00:04:08,000 --> 00:04:11,000
It's kind of floaty and we're going to fix it quickly.

63
00:04:12,000 --> 00:04:15,000
We probably going to balance this later again.

64
00:04:15,000 --> 00:04:20,000
But for now, let's just change gravity scale to four and see this.

65
00:04:21,000 --> 00:04:22,000
Now his jump is better.

66
00:04:23,000 --> 00:04:26,000
Maybe gravity skill to three and jump force to 12.

67
00:04:28,000 --> 00:04:31,000
Yeah, This feels like how it feels.

68
00:04:32,000 --> 00:04:34,000
And we cannot do endless jump anymore.

69
00:04:34,000 --> 00:04:35,000
Perfect.

70
00:04:35,000 --> 00:04:36,000
Now we done here.

71
00:04:36,000 --> 00:04:39,000
Let's do a bit of cleanup after work.

72
00:04:39,000 --> 00:04:40,000
It's going to be very simple.

73
00:04:40,000 --> 00:04:46,000
First, I want to create header and let's put square brackets and type header.

74
00:04:46,000 --> 00:04:52,000
Then in the parentheses we need to give a name for the header and it's going to be something like collision

75
00:04:53,000 --> 00:04:54,000
info.

76
00:04:54,000 --> 00:04:56,000
This will make it look better.

77
00:04:56,000 --> 00:04:58,000
In the inspector.

78
00:04:59,000 --> 00:05:01,000
If we go back to Unity, we can see it.

79
00:05:01,000 --> 00:05:02,000
Can we?

80
00:05:02,000 --> 00:05:03,000
We cannot.

81
00:05:04,000 --> 00:05:11,000
We cannot see it because first variable after header is private and to make it feasible, it should

82
00:05:11,000 --> 00:05:13,000
be public or serialized.

83
00:05:13,000 --> 00:05:15,000
So let's just move this below and save it.

84
00:05:20,000 --> 00:05:20,000
Good.

85
00:05:20,000 --> 00:05:26,000
Now we can see collision info and I probably misspelled that, but it's arranged a bit better.

86
00:05:26,000 --> 00:05:33,000
We don't need to see this boolean anymore so we can go and type private and the run began.

87
00:05:33,000 --> 00:05:34,000
Move it below.

88
00:05:35,000 --> 00:05:40,000
I can move this rigid body above and I'll make Heather move in for.

89
00:05:41,000 --> 00:05:41,000
Right.

90
00:05:42,000 --> 00:05:45,000
It looks much cleaner, much more understandable.

91
00:05:46,000 --> 00:05:49,000
Let's go back to code and change something else here.

92
00:05:49,000 --> 00:05:52,000
Doesn't make a big difference, but will help us in the future.

93
00:05:52,000 --> 00:05:54,000
Let's select this line of code.

94
00:05:54,000 --> 00:05:54,000
Click out.

95
00:05:54,000 --> 00:05:57,000
Enter Extract Method.

96
00:05:59,000 --> 00:06:04,000
Check lesion, and I want to be sure I did not misspell that at both places.

97
00:06:04,000 --> 00:06:07,000
So let me do this lesion.

98
00:06:08,000 --> 00:06:10,000
I'll just copy it from here.

99
00:06:11,000 --> 00:06:13,000
Yeah, like that.

100
00:06:13,000 --> 00:06:15,000
So this one is correct, I suppose.

101
00:06:15,000 --> 00:06:16,000
Yeah.

102
00:06:16,000 --> 00:06:19,000
And I copy this and paste it in here.

103
00:06:20,000 --> 00:06:21,000
Okay.

104
00:06:21,000 --> 00:06:23,000
Now we have update that is clean.

105
00:06:23,000 --> 00:06:27,000
Just a couple of functions doing stuff, and we have it clean here.

106
00:06:27,000 --> 00:06:27,000
Cool.

107
00:06:27,000 --> 00:06:29,000
We can move to next video.

