1 00:00:04,180 --> 00:00:05,110 ‫Welcome back. 2 00:00:05,140 --> 00:00:08,710 ‫In this video we will have a look at is trigger. 3 00:00:08,710 --> 00:00:11,830 ‫And you have seen physical bodies already. 4 00:00:11,830 --> 00:00:14,300 ‫You have seen colliders, all those kind of things. 5 00:00:14,320 --> 00:00:19,810 ‫Now let's add a new component, which is a trigger and it's actually not even a new component. 6 00:00:19,810 --> 00:00:23,680 ‫And that's the beauty, because it is simply the collider itself. 7 00:00:23,800 --> 00:00:26,140 ‫So you can make a collider is trigger. 8 00:00:26,140 --> 00:00:30,130 ‫For example, our player could be a trigger or a new object. 9 00:00:30,130 --> 00:00:34,780 ‫So let's create a new object into our game, a 3D object. 10 00:00:35,140 --> 00:00:36,730 ‫Let's go with a cube. 11 00:00:36,880 --> 00:00:41,500 ‫And what I will do with the cube, I will change the position to reset. 12 00:00:41,500 --> 00:00:46,410 ‫So I'm going to reset it and I'm going to drag it a little bit up and to the side. 13 00:00:46,420 --> 00:00:53,410 ‫Now let's change the scale of Y to, let's say five and the scale of X two. 14 00:00:54,550 --> 00:00:56,020 ‫Let's go with three. 15 00:00:56,230 --> 00:01:05,710 ‫So now we will have a wall here and maybe it's better if I move the wall to the side and change the 16 00:01:05,710 --> 00:01:08,680 ‫X to one and to Z two. 17 00:01:09,010 --> 00:01:10,030 ‫Let's go with four. 18 00:01:10,090 --> 00:01:19,300 ‫So now I have this huge ball here and what I want to happen is if I walk through or if the ball passes 19 00:01:19,300 --> 00:01:22,600 ‫into this cube, I want something to be triggered. 20 00:01:22,600 --> 00:01:24,310 ‫So I want something to happen. 21 00:01:24,310 --> 00:01:28,330 ‫For example, I want the whole thing to disappear. 22 00:01:28,330 --> 00:01:32,260 ‫So let's say I want the wall to disappear, which is this cube. 23 00:01:32,650 --> 00:01:34,930 ‫So let's give it a is trigger. 24 00:01:34,930 --> 00:01:37,600 ‫The cube itself gets an is trigger. 25 00:01:37,840 --> 00:01:38,710 ‫Let's have a look. 26 00:01:38,710 --> 00:01:48,220 ‫What that does is it deactivates its collider in terms of it's not a wall anymore so it's not a wall 27 00:01:48,220 --> 00:01:49,990 ‫that we cannot pass through anymore. 28 00:01:49,990 --> 00:01:52,420 ‫So now as you can see, our ball simply ignores it. 29 00:01:52,420 --> 00:01:53,650 ‫It just walks through. 30 00:01:54,100 --> 00:02:03,550 ‫So if we want this wall to do anything, whenever something passes it or hits it, then we need to create 31 00:02:03,550 --> 00:02:04,540 ‫a new script. 32 00:02:04,720 --> 00:02:10,090 ‫So I'm just going to call the script trigger script. 33 00:02:10,270 --> 00:02:16,210 ‫So let's go ahead and create a new script, right click create and then see sharp script and call it 34 00:02:16,210 --> 00:02:17,380 ‫trigger script. 35 00:02:18,070 --> 00:02:21,730 ‫Now I'm going to assign that trigger script to my cube. 36 00:02:22,180 --> 00:02:28,060 ‫So let's go ahead and add the component scripts and trigger script. 37 00:02:28,660 --> 00:02:31,110 ‫And now let's open trigger script up. 38 00:02:32,230 --> 00:02:36,340 ‫And there is only one thing that I need in here, so I won't need to start an update method. 39 00:02:36,700 --> 00:02:41,980 ‫And again, it's not that important that you understand the code in itself, but it's more important 40 00:02:41,980 --> 00:02:43,630 ‫that you understand the principles. 41 00:02:43,840 --> 00:02:52,480 ‫So whenever something needs to happen, when our object is hit by another object, we need to use the 42 00:02:52,480 --> 00:02:55,510 ‫on trigger enter method. 43 00:02:55,510 --> 00:02:57,040 ‫There's a method called on trigger. 44 00:02:57,040 --> 00:03:03,880 ‫Enter is called when the collider other enters the trigger and other could be our player for example. 45 00:03:04,270 --> 00:03:05,740 ‫So let's do that. 46 00:03:05,740 --> 00:03:07,060 ‫I just pressed enter. 47 00:03:07,060 --> 00:03:11,560 ‫I chose the on trigger entry method and now it's created for me automatically here. 48 00:03:12,100 --> 00:03:18,910 ‫So what I can enter here is, for example, print triggered. 49 00:03:21,010 --> 00:03:26,290 ‫So I'm just gonna print something onto the console and you will see what that does in a second. 50 00:03:26,890 --> 00:03:28,210 ‫So let's do that. 51 00:03:28,210 --> 00:03:31,780 ‫So now our cube, it's really important that our cube has this trigger script. 52 00:03:31,780 --> 00:03:33,520 ‫Now we run the code. 53 00:03:35,790 --> 00:03:39,330 ‫And let's pass through our trigger. 54 00:03:39,330 --> 00:03:42,580 ‫And as you can see on the console here, it says Trigger. 55 00:03:42,600 --> 00:03:44,490 ‫So now let's trigger once again. 56 00:03:44,820 --> 00:03:46,500 ‫Let's try to get the ball back. 57 00:03:47,670 --> 00:03:49,830 ‫There we are and let's hit it again. 58 00:03:53,280 --> 00:03:54,900 ‫You can see it's called three. 59 00:03:54,930 --> 00:03:59,130 ‫It was called three times already, and that's because we passed through it three times. 60 00:03:59,880 --> 00:04:04,800 ‫What you could use that for is, for example, to get rid of this whole game object. 61 00:04:05,190 --> 00:04:06,210 ‫So let's do that. 62 00:04:06,210 --> 00:04:12,690 ‫Let's go back into the code and we call Trigger now and we also call the destroy method. 63 00:04:13,530 --> 00:04:16,470 ‫And I'm just going to give game object. 64 00:04:16,470 --> 00:04:22,290 ‫So what that will do and I'm just going to use game object as the parameter, what the parameter is, 65 00:04:22,290 --> 00:04:23,660 ‫what the destroy method is. 66 00:04:23,670 --> 00:04:26,890 ‫If you don't know those things yet, we will get into that later. 67 00:04:26,910 --> 00:04:34,140 ‫So the destroy method, what it will do, it will simply destroy the game object that contains this 68 00:04:34,140 --> 00:04:35,130 ‫trigger script. 69 00:04:35,130 --> 00:04:37,770 ‫So that's what this line here says. 70 00:04:38,850 --> 00:04:43,020 ‫So let's have a look how this looks in game. 71 00:04:45,060 --> 00:04:49,170 ‫So I'm going to simply trigger this event. 72 00:04:49,170 --> 00:04:53,310 ‫And you see, our wall has disappeared. 73 00:04:53,640 --> 00:05:01,560 ‫So if we set up our game slightly differently, so I'm just going to go ahead and position my ball at 74 00:05:01,560 --> 00:05:05,370 ‫the bottom straight away and move it to the left. 75 00:05:09,340 --> 00:05:15,610 ‫And now I'm going to move my cube a little bit and I'm going to duplicate that cube multiple times, 76 00:05:15,610 --> 00:05:19,300 ‫and now I move the cubes one after the other. 77 00:05:20,380 --> 00:05:22,090 ‫So let's just move them. 78 00:05:23,800 --> 00:05:25,480 ‫And this one as well. 79 00:05:25,480 --> 00:05:28,630 ‫And the last one as well. 80 00:05:28,780 --> 00:05:38,020 ‫So now if I start it off, what will happen is that one after the other will disappear that we are. 81 00:05:38,020 --> 00:05:45,430 ‫I simply duplicated them with using control dx or command d based on which system you are using. 82 00:05:45,430 --> 00:05:48,490 ‫So right click duplicate is another option. 83 00:05:48,490 --> 00:05:53,560 ‫So if you duplicate it this way, then the object will be duplicated at the same position. 84 00:05:53,560 --> 00:05:56,230 ‫So that's why you cannot see that there are two objects. 85 00:05:56,230 --> 00:06:00,490 ‫So you need to move one in order to see that there are two objects, in fact. 86 00:06:00,880 --> 00:06:02,860 ‫So let's go ahead and do that again. 87 00:06:04,030 --> 00:06:06,880 ‫And now, as you can see, the destroy method is called. 88 00:06:07,120 --> 00:06:11,110 ‫Now, what we could do is deactivate. 89 00:06:11,110 --> 00:06:18,640 ‫That is trigger onto any of our objects or all of them are objects, at least of our cube objects. 90 00:06:18,820 --> 00:06:24,640 ‫So let's do that real quick, except for the last two. 91 00:06:24,640 --> 00:06:31,870 ‫And now what I want to add as well is a rigid body to all of them, or except while the first ones at 92 00:06:31,870 --> 00:06:32,320 ‫least. 93 00:06:32,320 --> 00:06:36,850 ‫So let's add a physics rigid body to cube cube. 94 00:06:36,850 --> 00:06:38,110 ‫One, two, three. 95 00:06:38,110 --> 00:06:39,520 ‫And that's it. 96 00:06:39,520 --> 00:06:40,720 ‫So physics. 97 00:06:42,060 --> 00:06:45,840 ‫Granted, Buddy, and add physics. 98 00:06:46,980 --> 00:06:51,810 ‫It's important that you use physics and not physics to DX because otherwise it won't work. 99 00:06:52,080 --> 00:06:53,820 ‫Now let's run it again. 100 00:06:54,720 --> 00:06:57,360 ‫And now let's try to hit it with our ball. 101 00:06:57,810 --> 00:07:03,000 ‫And as you can see, the first one still had a trigger. 102 00:07:03,310 --> 00:07:04,860 ‫Now, boom, boom. 103 00:07:05,100 --> 00:07:06,930 ‫And those two, they just fall off. 104 00:07:06,960 --> 00:07:11,430 ‫So the ones that had a trigger, they disappeared and the ones that didn't have a trigger. 105 00:07:11,430 --> 00:07:12,990 ‫So for five. 106 00:07:13,170 --> 00:07:14,890 ‫And I think that's it. 107 00:07:14,910 --> 00:07:17,490 ‫They had a trigger still, so they disappeared. 108 00:07:17,490 --> 00:07:19,620 ‫But the other ones didn't disappear. 109 00:07:20,770 --> 00:07:27,250 ‫Because they didn't have the trigger method activated and that's why they weren't destroyed. 110 00:07:28,090 --> 00:07:31,690 ‫So now let's get rid of the trigger method from each of them. 111 00:07:35,420 --> 00:07:40,220 ‫And what that will allow us to do is to have a little domino game. 112 00:07:41,690 --> 00:07:44,690 ‫So let's go ahead and hit them off. 113 00:07:44,780 --> 00:07:47,150 ‫Maybe we should make our player a bit bigger. 114 00:07:47,630 --> 00:07:49,130 ‫I'm going to pause the game again. 115 00:07:49,310 --> 00:07:51,860 ‫Go ahead and change the radius. 116 00:07:51,860 --> 00:07:57,020 ‫Or actually the scale of our player to, let's say three times three times three. 117 00:07:57,320 --> 00:07:59,720 ‫And let's drag it up a little bit. 118 00:08:03,290 --> 00:08:06,740 ‫And now what we could do is. 119 00:08:08,320 --> 00:08:10,450 ‫Go to here, you can use global or local. 120 00:08:10,450 --> 00:08:16,690 ‫So now we are global and now I can move your camera towards the front and move it up a little bit. 121 00:08:17,080 --> 00:08:19,840 ‫So now you have a better angle at things. 122 00:08:19,840 --> 00:08:21,280 ‫So let's start it again. 123 00:08:23,190 --> 00:08:24,390 ‫And now let's move it. 124 00:08:24,390 --> 00:08:28,170 ‫And as you can see, not all of them are moving. 125 00:08:28,350 --> 00:08:29,600 ‫Why is that one not moving? 126 00:08:29,610 --> 00:08:31,200 ‫Maybe you can figure it out. 127 00:08:31,930 --> 00:08:35,370 ‫As you can see now, I can move it just at the edge of it. 128 00:08:35,610 --> 00:08:37,140 ‫Maybe you know why it's not moving. 129 00:08:37,650 --> 00:08:38,610 ‫I hope you know it. 130 00:08:38,910 --> 00:08:41,430 ‫It is because it doesn't have a rigid body. 131 00:08:41,430 --> 00:08:42,060 ‫So Q. 132 00:08:42,090 --> 00:08:48,450 ‫Q one, two and three have a rigid body, but they are not here in the order of their naming, so. 133 00:08:48,480 --> 00:08:50,010 ‫Q One is the first one. 134 00:08:50,040 --> 00:08:51,780 ‫The cube is the right one. 135 00:08:53,120 --> 00:08:56,120 ‫Two is this one and three is that one. 136 00:08:56,120 --> 00:09:01,400 ‫So the fourth is the one to the right and the fifth is this one. 137 00:09:01,400 --> 00:09:03,740 ‫And those two, they don't have a physics body. 138 00:09:03,740 --> 00:09:07,100 ‫That's why they cannot be influenced by physics. 139 00:09:07,430 --> 00:09:08,930 ‫So let's change that. 140 00:09:08,930 --> 00:09:11,120 ‫Let's give them a physics body as well. 141 00:09:13,200 --> 00:09:15,120 ‫Physics and rigid body. 142 00:09:15,150 --> 00:09:17,160 ‫Now, let's go ahead and play that again. 143 00:09:17,310 --> 00:09:20,400 ‫Or maybe let's even get rid of this central one. 144 00:09:20,970 --> 00:09:27,540 ‫So let's delete this that cube here, delete and let's run it again. 145 00:09:29,130 --> 00:09:30,180 ‫And now let's hit it. 146 00:09:30,660 --> 00:09:33,720 ‫And as you can see, we have this little domino game here. 147 00:09:34,800 --> 00:09:36,620 ‫And let's try to get on top of it. 148 00:09:36,630 --> 00:09:36,990 ‫Yeah. 149 00:09:37,080 --> 00:09:37,850 ‫Perfect. 150 00:09:37,860 --> 00:09:42,000 ‫Our ball can with its force, it can move on top of it. 151 00:09:42,000 --> 00:09:44,550 ‫It can move around those objects. 152 00:09:44,550 --> 00:09:52,380 ‫And now you could go ahead and play, create your own little game, a little domino game or a little 153 00:09:52,380 --> 00:09:59,160 ‫collection game where you can collect game objects and make them disappear with the destroy method. 154 00:09:59,790 --> 00:10:00,210 ‫All right. 155 00:10:00,210 --> 00:10:02,250 ‫So that's it for now. 156 00:10:02,490 --> 00:10:07,080 ‫Regarding Collider's rigid body and its trigger. 157 00:10:07,110 --> 00:10:09,930 ‫We will use those extensively in our games. 158 00:10:09,930 --> 00:10:14,460 ‫So no worries if you don't really get a grasp of it completely yet. 159 00:10:14,460 --> 00:10:18,840 ‫But we will get there and you will see it within real games. 160 00:10:19,140 --> 00:10:20,760 ‫So see you in the next video.