1
00:00:03,070 --> 00:00:03,660
Hello again.

2
00:00:03,670 --> 00:00:09,700
In this lecture we're using on collision, enter within our code to know when we bump, bump into something

3
00:00:09,700 --> 00:00:11,530
and we can make our code do something.

4
00:00:11,530 --> 00:00:15,910
In this case, we're just printing to the console that one object bumped into another object.

5
00:00:15,910 --> 00:00:17,620
So let's jump in and get started.

6
00:00:17,630 --> 00:00:22,960
First thing we need to do in this process is create ourselves some code that says we know when things

7
00:00:22,960 --> 00:00:29,200
bumped into other things and we're interested in saying, when did the player bump into anything else

8
00:00:29,200 --> 00:00:29,740
in the world?

9
00:00:29,740 --> 00:00:34,960
So I'm going to add a script to the player that says, Let me know when I bumped into something.

10
00:00:34,960 --> 00:00:39,670
And basically you could add all of this code into the one script you've got already.

11
00:00:39,670 --> 00:00:44,020
You could have a whole ton of different functionality in there, but that's bad practice because then

12
00:00:44,020 --> 00:00:45,880
you don't know what is going on.

13
00:00:45,880 --> 00:00:47,830
It makes this script really long.

14
00:00:47,830 --> 00:00:52,840
There's a lot to wade through, so it's better practice for each functionality that you need to make

15
00:00:52,840 --> 00:00:53,380
a new script.

16
00:00:53,380 --> 00:00:57,580
So let's right click in our assets area, create a C-sharp script.

17
00:00:57,580 --> 00:01:04,209
I'm going to call this collision hit enter and then open up collision from within here.

18
00:01:04,209 --> 00:01:09,100
We're not going to need start or updates, so I'm going to highlight all of those things from the comment

19
00:01:09,100 --> 00:01:09,700
up above.

20
00:01:09,700 --> 00:01:15,040
Start all the way down to the second last curly brace you see in here and delete that.

21
00:01:15,040 --> 00:01:20,170
So now we just have the open and the closed curly braces that say, what would you like to have in this

22
00:01:20,170 --> 00:01:22,150
class that's called collision.

23
00:01:22,330 --> 00:01:22,900
Okay.

24
00:01:22,900 --> 00:01:24,880
Well, tab in one little bit just there.

25
00:01:24,880 --> 00:01:32,110
And I'm going to use another in-built method that unity has similar to start and update.

26
00:01:32,110 --> 00:01:37,840
We've got something called on collision enter and we need the 2D version.

27
00:01:37,840 --> 00:01:43,180
Just like in the last lecture where we're talking about sometimes we use non 2D, sometimes we use 2D.

28
00:01:43,180 --> 00:01:47,590
When we're working in a 2D project with sprites, we need to use the 2D version of things.

29
00:01:47,590 --> 00:01:52,630
And so I clicked on that to complete it and we get a whole bunch of other stuff in here as part of the

30
00:01:52,630 --> 00:01:53,410
completion.

31
00:01:53,410 --> 00:01:58,960
So in general I delete the word private from my methods.

32
00:01:58,960 --> 00:02:05,230
Basically, private is a way of saying this can only be used in this class, it can't be used somewhere

33
00:02:05,230 --> 00:02:05,410
else.

34
00:02:05,410 --> 00:02:10,780
We can't update or change or modify or call this from somewhere else.

35
00:02:10,780 --> 00:02:11,830
That's what private does.

36
00:02:11,830 --> 00:02:16,180
But if we remove the private key word, it has exactly the same impact.

37
00:02:16,180 --> 00:02:18,310
So I remove it just to make things cleaner.

38
00:02:18,310 --> 00:02:21,520
But you can absolutely leave it there if you want that private keyword.

39
00:02:21,520 --> 00:02:27,040
And now what I also do is I fix up my curly bracket there so that I have it nice and neatly aligned.

40
00:02:27,040 --> 00:02:29,740
You can totally leave it on the line that it's on like that.

41
00:02:29,740 --> 00:02:34,930
That's totally fine because whitespace doesn't matter in C sharp in unity.

42
00:02:34,930 --> 00:02:39,700
So I could have it like that if I wanted to because the compiler is just going to ignore all that stuff.

43
00:02:39,700 --> 00:02:40,460
It doesn't care about.

44
00:02:40,460 --> 00:02:41,650
It doesn't care about whitespace.

45
00:02:41,650 --> 00:02:43,150
We can have our lines like that.

46
00:02:43,150 --> 00:02:45,010
We could do it like this if we wanted.

47
00:02:45,010 --> 00:02:47,830
We could have a whole space in between there.

48
00:02:47,830 --> 00:02:49,120
I think you get the picture.

49
00:02:49,120 --> 00:02:50,890
Riding it like this is totally valid.

50
00:02:50,890 --> 00:02:55,030
It'll work, but it looks a little bit ugly, I think, and a little bit messy.

51
00:02:55,030 --> 00:02:57,190
So I'm just going to undo all these things.

52
00:02:57,190 --> 00:03:01,270
And for me, what I like is to have my curly braces formatted like this.

53
00:03:01,270 --> 00:03:06,040
So I do that any time we get this kind of auto completion a lot, reformat it like that.

54
00:03:06,040 --> 00:03:10,120
And we also have some information that's being passed in collision to the other.

55
00:03:10,120 --> 00:03:16,450
All you need to know for now is that if we bump into something when we have a collision event, we can

56
00:03:16,450 --> 00:03:18,910
get information about the thing we bumped into.

57
00:03:18,910 --> 00:03:22,000
We can find its name, we can find details about it.

58
00:03:22,000 --> 00:03:25,180
We can change it if we want, says kind of cool.

59
00:03:25,180 --> 00:03:28,180
We don't actually need that for the little exercise we're doing now.

60
00:03:28,180 --> 00:03:29,740
So we could absolutely delete that.

61
00:03:29,740 --> 00:03:34,210
We don't need to have that information in there, but I'm going to leave that in, just undo and leave

62
00:03:34,210 --> 00:03:36,010
it in there because there's no harm in having it.

63
00:03:36,010 --> 00:03:38,380
But I just want you to know that this does a thing.

64
00:03:38,380 --> 00:03:40,750
We just don't need to use that thing just yet.

65
00:03:40,750 --> 00:03:42,490
So I have a little challenge for you here.

66
00:03:42,490 --> 00:03:45,070
This is something that we did in the very first section of the course.

67
00:03:45,070 --> 00:03:47,230
We printed something to the console.

68
00:03:47,230 --> 00:03:53,110
So the challenge I've got for you is to print something to the console using debug log print, a witty

69
00:03:53,110 --> 00:03:55,720
message to the console when we collide with something.

70
00:03:55,720 --> 00:03:56,770
So pause the video.

71
00:03:56,770 --> 00:03:57,850
Give that a go now.

72
00:03:59,190 --> 00:03:59,510
Okay.

73
00:03:59,520 --> 00:04:02,640
So we need to add this within our code block.

74
00:04:02,640 --> 00:04:09,470
So when we collide, we'll add something, we use debug log parentheses and then quotation marks.

75
00:04:09,480 --> 00:04:10,440
What am I going to say?

76
00:04:10,440 --> 00:04:11,880
I'm just going to say, ouch.

77
00:04:12,630 --> 00:04:13,350
Is that witty?

78
00:04:13,350 --> 00:04:13,950
I don't know.

79
00:04:13,950 --> 00:04:16,070
But I think it's kind of funny myself.

80
00:04:16,079 --> 00:04:20,700
Ouch semicolon save now back over into unity.

81
00:04:20,700 --> 00:04:23,070
So there's one last step in this process.

82
00:04:23,070 --> 00:04:25,230
Hopefully you figure this out when you're going through the challenge.

83
00:04:25,230 --> 00:04:29,550
But if not, what's the last step we need to do to print out to the console?

84
00:04:30,400 --> 00:04:36,850
Well, we need to click on our player and we need to add that script as a component so I can do it a

85
00:04:36,850 --> 00:04:37,480
couple of ways.

86
00:04:37,480 --> 00:04:42,400
One, I can click in here, grab collision, drag it in, or I can click on Add component and I can

87
00:04:42,400 --> 00:04:43,690
add collision.

88
00:04:43,690 --> 00:04:46,600
And you see our first one here is Collision, the script.

89
00:04:46,600 --> 00:04:50,350
Click on that and we have a collision script as a component on our vehicle.

90
00:04:50,350 --> 00:04:56,470
Now we'll click on console again and if we've set this up correctly and I click on play, turn towards

91
00:04:56,470 --> 00:05:01,240
my object, charge, blink, and I've bumped into it and it says Ouch.

92
00:05:01,240 --> 00:05:06,280
And you can see here in the console because we've got collapsed turned on, we've got a number all the

93
00:05:06,280 --> 00:05:12,220
way over in the right hand side that says how many times we've called that and printed that to the console.

94
00:05:12,220 --> 00:05:16,690
So if I back up and bounce back up and bounce back up and bounce, you can see that the number is going

95
00:05:16,690 --> 00:05:18,280
up to four.

96
00:05:18,280 --> 00:05:25,150
Now, what I could do is click on Collapse and then every time I bump into it, it will make a new line

97
00:05:25,150 --> 00:05:25,750
in the console.

98
00:05:25,750 --> 00:05:29,110
I've kind of pushed him all the way off the screen and see if I can push him back in again.

99
00:05:29,110 --> 00:05:32,080
Might need to cheat by looking at the scene window.

100
00:05:32,080 --> 00:05:32,740
Yep, yep.

101
00:05:33,040 --> 00:05:33,370
Okay.

102
00:05:33,430 --> 00:05:36,790
Push back into the world and you can see it'll add a new line.

103
00:05:36,790 --> 00:05:40,060
If I click on Collapse, it'll collapse it all so that we see how many is in there.

104
00:05:40,060 --> 00:05:40,930
Just a little trick there.

105
00:05:40,930 --> 00:05:46,720
In terms of printing the console, it's really handy to use debug statements printing to the console

106
00:05:46,720 --> 00:05:48,580
when you're doing things, particularly with collisions.

107
00:05:48,580 --> 00:05:52,230
So, you know, collisions actually working and you're not sitting there scratching your head because

108
00:05:52,240 --> 00:05:55,210
it's saying, Yep, I bumped into a thing which is really handy to do.

109
00:05:55,210 --> 00:05:59,320
So that's the fundamentals of using collision to make things happen.

110
00:05:59,320 --> 00:06:00,340
In our game.

111
00:06:00,340 --> 00:06:04,120
I have a rigid body on at least one of these objects.

112
00:06:04,120 --> 00:06:05,710
In this case, we have a rigid body on both.

113
00:06:05,710 --> 00:06:11,590
But you need to read your body on at least one, and we need to have our colliders on both of these

114
00:06:11,590 --> 00:06:12,010
objects.

115
00:06:12,010 --> 00:06:17,470
Both objects need to have collider and now we can say when we bump into something, we can make our

116
00:06:17,470 --> 00:06:19,090
code do particular things.

117
00:06:19,090 --> 00:06:23,050
For the moment we're just printing out to the console, but we can do more interesting things in the

118
00:06:23,050 --> 00:06:26,020
future when we bump into things within our world.

119
00:06:26,020 --> 00:06:26,650
So there we go.

120
00:06:26,680 --> 00:06:27,910
That's using on collision.

121
00:06:27,910 --> 00:06:29,560
Enter within our game.

122
00:06:29,560 --> 00:06:30,700
I'll see you in the next lecture.

