1
00:00:02,990 --> 00:00:08,090
In this lecture we're looking at triggers and using on trigger enter so that when the player crosses

2
00:00:08,090 --> 00:00:12,100
over a trigger volume, it will print something to the console.

3
00:00:12,110 --> 00:00:13,150
Let's get started.

4
00:00:13,160 --> 00:00:17,990
Okay, let's start this process by adding another game object that we can use as our trigger.

5
00:00:17,990 --> 00:00:24,470
So I want you to pause a video and add a box or something rectangular that we can use as a trigger.

6
00:00:25,730 --> 00:00:26,060
Okay.

7
00:00:26,060 --> 00:00:33,100
So in the hierarchy, right click to the object physics and we will add a square and square.

8
00:00:33,110 --> 00:00:37,640
We will move a little bit above or I'll move a little bit above and I'm going to stretch it a little

9
00:00:37,640 --> 00:00:38,070
bit as well.

10
00:00:38,080 --> 00:00:43,520
Just hit R and stretch it a little bit that way, change the color so we differentiate it.

11
00:00:43,520 --> 00:00:48,500
So clicking on my inspector, you can see that I've closed some things in here so it remembers.

12
00:00:48,500 --> 00:00:49,250
I wanted it closed.

13
00:00:49,250 --> 00:00:50,240
I'm going to open that up again.

14
00:00:50,240 --> 00:00:52,760
The Sprite renderer, click on the white area.

15
00:00:52,760 --> 00:00:56,510
Change this to a pink I think for our trigger.

16
00:00:56,540 --> 00:00:58,850
What do you think the next step in the process is?

17
00:00:58,850 --> 00:01:04,370
What do we need to add to this thing to know when we've passed through it, over it, around it, we

18
00:01:04,370 --> 00:01:06,590
need to add a collider.

19
00:01:06,590 --> 00:01:10,030
What sort of collider need to add a box collider to DH?

20
00:01:10,070 --> 00:01:15,770
That box collider to DS, you can see, has taken the exact shape of our object, which is great.

21
00:01:15,770 --> 00:01:21,050
Now let's see what happens when we drive our vehicle towards that new thing we've added.

22
00:01:21,050 --> 00:01:25,010
If I can drive correctly and we drive into it bump, okay.

23
00:01:25,040 --> 00:01:27,140
It doesn't move because there's no rigid body on it.

24
00:01:27,140 --> 00:01:27,620
That's fine.

25
00:01:27,620 --> 00:01:30,500
We don't need it to move, but we can bump into it.

26
00:01:30,500 --> 00:01:35,750
But what if I want to say this is like a start or finish line or this is some sort of pickup I want

27
00:01:35,750 --> 00:01:41,660
to drive over or some sort of indicator that says that the player passed from one zone to another.

28
00:01:41,660 --> 00:01:43,160
I don't want to bounce into it.

29
00:01:43,160 --> 00:01:46,430
I want to drive through it, but I still want to know when we've driven through it.

30
00:01:46,430 --> 00:01:52,910
There's one really simple thing if we've clicked on our square just here within our collider, you see

31
00:01:52,910 --> 00:01:53,600
is trigger.

32
00:01:53,600 --> 00:01:59,600
By clicking on that, we've now said that we want this collider to be a trigger sort of collider.

33
00:01:59,630 --> 00:02:02,660
Normally I wouldn't call it a trigger collider, I just call it a trigger.

34
00:02:02,660 --> 00:02:06,440
But you can see we've got a collider and we've said that collider is a trigger.

35
00:02:06,440 --> 00:02:13,250
Now when I click on play, I'm going to drive my car and my car is going to pass right over or through

36
00:02:13,250 --> 00:02:15,680
and not bumping into it, not getting stopped by it.

37
00:02:15,680 --> 00:02:17,030
Now, why is it going underneath?

38
00:02:17,030 --> 00:02:20,690
While we talked about this in the first section of the course, it's because of our layer order.

39
00:02:20,690 --> 00:02:25,010
This square is at Layer Order zero and our player is also at Layer Order zero.

40
00:02:25,010 --> 00:02:25,940
So it's a little bit random.

41
00:02:25,940 --> 00:02:26,870
Which one is going to be on top?

42
00:02:26,870 --> 00:02:28,040
Which one's going to be below?

43
00:02:28,040 --> 00:02:32,030
I'd like the player to always be on top of these things, so I'm going to change the order in layer

44
00:02:32,030 --> 00:02:34,700
for the player cruising drive to ten.

45
00:02:34,700 --> 00:02:40,070
Now when we drive over this, it should drive and actually you'll see it driving over there we go over

46
00:02:40,070 --> 00:02:41,090
the object, which is cool.

47
00:02:41,090 --> 00:02:41,960
What's the next step?

48
00:02:41,960 --> 00:02:47,330
Well, we need to jump into our script, into our collision CSS script, similarly to the way we said

49
00:02:47,330 --> 00:02:52,700
on Collision Enter, we're going to use a new method which is on trigger enter 2D.

50
00:02:52,700 --> 00:02:56,790
And I'm going to give this to you as a challenge to see how well you've been paying attention.

51
00:02:56,930 --> 00:03:03,290
Print When we trigger something so you use on trigger enter to DD and debug dot log to print yet another

52
00:03:03,290 --> 00:03:06,710
witty message to the console when we trigger something.

53
00:03:06,710 --> 00:03:08,360
So there's is your challenge pause video.

54
00:03:08,360 --> 00:03:11,390
Take that on, test that it works and I'll see you back here when you're done.

55
00:03:13,540 --> 00:03:13,900
Right.

56
00:03:13,900 --> 00:03:18,850
So in our code, I'm going to give myself a couple of new lines to give some space.

57
00:03:18,850 --> 00:03:26,620
I will type in on trigger, enter, find the 2D version, and it adds in all this stuff as it did before.

58
00:03:26,620 --> 00:03:31,780
I'm just going to remove private because that's what I like to do and tidy up my curly braces.

59
00:03:31,780 --> 00:03:38,680
Now from within this area, I'm going to type debug log parentheses, quotation marks and I will say,

60
00:03:38,680 --> 00:03:40,900
what was that?

61
00:03:41,620 --> 00:03:44,860
Not very witty once again, but I've got ouch when I bump into it.

62
00:03:44,860 --> 00:03:45,550
And what was that.

63
00:03:45,550 --> 00:03:51,280
Because the player pass through something, save back over into unity, make sure we're on the console

64
00:03:51,280 --> 00:03:52,750
so we can see what's going on there.

65
00:03:52,750 --> 00:03:59,680
Click play and we'll drive over the pink area bump and it says, What was that the moment I pass into

66
00:03:59,680 --> 00:03:59,770
it?

67
00:03:59,770 --> 00:04:07,870
So you can see right about now it increases my number in terms of my count bump just about there and

68
00:04:07,870 --> 00:04:11,290
it only does it when we first touch it the whole way, passing through.

69
00:04:11,290 --> 00:04:13,000
Nothing, nothing, nothing, nothing, nothing.

70
00:04:13,000 --> 00:04:13,900
And we leave.

71
00:04:13,900 --> 00:04:16,360
There's another method which is on trigger exit.

72
00:04:16,360 --> 00:04:20,110
If you want to know when someone leaves a particular area, you can use that method.

73
00:04:20,110 --> 00:04:23,770
But I'm going to bump, touch it and then it will increase.

74
00:04:23,770 --> 00:04:29,470
The other thing you could do is you could click on your square and turn off the sprite renderer so the

75
00:04:29,470 --> 00:04:31,150
player doesn't even know there's a thing there.

76
00:04:31,150 --> 00:04:32,020
I drive through it.

77
00:04:32,020 --> 00:04:32,740
I know it's there.

78
00:04:32,740 --> 00:04:33,640
You know it's there.

79
00:04:33,640 --> 00:04:34,510
That's our little secret.

80
00:04:34,510 --> 00:04:38,620
But see how my count is still increasing because I'm driving over that area.

81
00:04:38,620 --> 00:04:43,780
So that's the way that we can also use a trigger volume in a way that the player doesn't even know.

82
00:04:43,780 --> 00:04:48,250
So there's plenty of occasions in games where we want to say, Did the player reach this point or did

83
00:04:48,250 --> 00:04:49,420
something go off screen?

84
00:04:49,420 --> 00:04:54,790
If it did, then do something to it, or did I go into a particular zone so triggers a super useful.

85
00:04:54,790 --> 00:04:58,210
And with that, my friend, you have the foundation of many games out there.

86
00:04:58,210 --> 00:05:03,610
You now know to have how to have the player go through a trigger volume and to have something happen

87
00:05:03,610 --> 00:05:04,960
in the code when that happens.

88
00:05:04,960 --> 00:05:10,060
And also for the player to bump into something and to have something happen in the code when that happens.

89
00:05:10,060 --> 00:05:15,880
So that really is the foundation of so many things that we do in our games, the triggers and colliders.

90
00:05:15,880 --> 00:05:16,930
So that's all for this lecture.

91
00:05:16,930 --> 00:05:18,220
I will see you in the next one.

