WEBVTT 1 00:00:05.696 --> 00:00:09.666 Game Advanced Prototype 4 2 00:00:09.666 --> 00:00:12.108 GCC Academy 3 00:00:27.554 --> 00:00:28.464 Hi 4 00:00:28.464 --> 00:00:30.970 I'm Youngho Lee, a shooting game development course instructor 5 00:00:30.970 --> 00:00:32.436 In this chapter 6 00:00:32.436 --> 00:00:35.317 We will go over Object collision processing 7 00:00:35.317 --> 00:00:37.386 enemy auto generation 8 00:00:37.386 --> 00:00:40.554 and DestroyZone for memory management 9 00:00:40.554 --> 00:00:43.000 Also, learn how to set requirements 10 00:00:43.000 --> 00:00:45.050 for processing collision in Unity 11 00:00:45.050 --> 00:00:47.000 and between object groups 12 00:00:47.000 --> 00:00:51.247 You will learn how to manage memory using this 13 00:00:51.247 --> 00:00:53.208 we will create a manager object 14 00:00:53.208 --> 00:00:55.564 to automatically generate enemies 15 00:00:56.099 --> 00:01:00.000 Object collision processing 16 00:01:00.594 --> 00:01:02.643 The 4th prototype 17 00:01:02.643 --> 00:01:07.000 Let's learn about object collision processing 18 00:01:07.000 --> 00:01:10.445 You need two conditions for object collision processing 19 00:01:10.445 --> 00:01:13.238 The collision between objects in Unity, 20 00:01:13.238 --> 00:01:19.257 when such objects are colliding each other 21 00:01:19.257 --> 00:01:22.950 two conditions are required 22 00:01:22.950 --> 00:01:25.000 First, look closely 23 00:01:25.000 --> 00:01:30.624 let's say in a certain space 24 00:01:30.624 --> 00:01:34.000 there is this object 25 00:01:34.000 --> 00:01:37.703 Then, in this space 26 00:01:37.703 --> 00:01:41.376 Let's make it 3d 27 00:01:41.376 --> 00:01:45.861 Let's also say this player moves to the right 28 00:01:45.861 --> 00:01:49.436 in this space, to the right in our perspective of looking at the screen 29 00:01:49.436 --> 00:01:51.337 This side is blocked by a wall 30 00:01:51.337 --> 00:01:55.515 So, this player runs in this direction 31 00:01:55.515 --> 00:01:56.921 Then, what would happen to this player? 32 00:01:56.921 --> 00:02:02.485 Will it go through the wall? or collide and bounce off? 33 00:02:02.485 --> 00:02:06.000 The player will collide and bounce off 34 00:02:06.000 --> 00:02:07.871 with bloody nose of course, generally 35 00:02:11.643 --> 00:02:13.000 But in certain cases 36 00:02:13.000 --> 00:02:17.653 the player could pass through the wall 37 00:02:17.653 --> 00:02:24.841 So, what about the case where this object hits the wall 38 00:02:24.841 --> 00:02:27.505 and can't pass through? 39 00:02:27.505 --> 00:02:31.594 The first condition comes into play here 40 00:02:31.594 --> 00:02:34.000 If the player is materialized like this 41 00:02:34.000 --> 00:02:37.386 When it runs this way, it will definitely hit the wall and bounce off 42 00:02:37.386 --> 00:02:40.663 But what if it's a ghost? 43 00:02:40.663 --> 00:02:43.000 What would it be like if it's was ghost? 44 00:02:43.000 --> 00:02:46.554 If it runs towards this wall, will it hit the wall and bounce off? 45 00:02:46.554 --> 00:02:52.000 Nope, it will pass through and move out of this space 46 00:02:52.000 --> 00:02:55.841 What kind of difference is there 47 00:02:55.841 --> 00:02:58.208 for the player to be blocked by the wall 48 00:02:58.208 --> 00:03:01.000 and for the ghost to be able to pass through? 49 00:03:01.000 --> 00:03:04.297 That's where the first condition comes into play 50 00:03:04.297 --> 00:03:07.198 The first condition is 51 00:03:07.198 --> 00:03:09.148 this player has a body 52 00:03:09.148 --> 00:03:12.238 that is made of bones and flesh 53 00:03:12.238 --> 00:03:15.455 But how about ghosts? They do not have a body 54 00:03:15.455 --> 00:03:17.805 Therefore, first, body, we call this 55 00:03:17.805 --> 00:03:20.000 Collider in a physics engine 56 00:03:22.911 --> 00:03:24.168 The Collider 57 00:03:24.168 --> 00:03:28.317 We call this Collider in English 58 00:03:28.317 --> 00:03:29.535 Collider 59 00:03:36.386 --> 00:03:42.158 Then, what is the second condition that is required? 60 00:03:42.158 --> 00:03:45.703 When we apply this to the player object 61 00:03:45.703 --> 00:03:48.651 if the player runs to the right 62 00:03:48.651 --> 00:03:49.594 left 63 00:03:49.594 --> 00:03:52.634 up, or down, then what happens? 64 00:03:52.634 --> 00:03:57.851 All these walls, walls that form this cube 65 00:03:57.851 --> 00:04:00.000 it can collide with these walls 66 00:04:00.000 --> 00:04:06.050 But then this wall on the left 67 00:04:06.050 --> 00:04:09.187 and then this wall on the right 68 00:04:09.187 --> 00:04:11.563 can these walls collide? 69 00:04:11.563 --> 00:04:13.396 In the space this player is in 70 00:04:13.396 --> 00:04:16.277 can the ceiling and the floor collide? 71 00:04:16.277 --> 00:04:18.386 We are not considering collapse 72 00:04:18.386 --> 00:04:22.376 In a normal case, it can't collide 73 00:04:22.376 --> 00:04:26.436 Then, there's one thing we want to know here 74 00:04:26.436 --> 00:04:28.663 How can the player here 75 00:04:28.663 --> 00:04:31.213 collide with the floor 76 00:04:31.213 --> 00:04:34.406 ceiling, or left wall 77 00:04:34.406 --> 00:04:37.455 but these walls can't collide with each other? 78 00:04:37.455 --> 00:04:40.703 The distinction here is 79 00:04:40.703 --> 00:04:43.000 walls cannot move 80 00:04:43.000 --> 00:04:46.356 but the player can move 81 00:04:46.356 --> 00:04:51.227 Therefore 82 00:04:51.227 --> 00:04:55.614 it should be able to move around 83 00:04:55.614 --> 00:04:57.337 When other things cannot move 84 00:04:57.337 --> 00:05:01.000 This object, at least one object should be able to move 85 00:05:01.000 --> 00:05:04.750 That moving object can collide 86 00:05:04.750 --> 00:05:06.762 even with fixed object 87 00:05:06.762 --> 00:05:10.535 Therefore, we need the object to be able to move 88 00:05:10.535 --> 00:05:14.340 and we call it Rigidbody in Unity 89 00:05:18.624 --> 00:05:21.841 These are all related to components 90 00:05:21.841 --> 00:05:24.960 Each game object needs to have a collider 91 00:05:24.960 --> 00:05:27.604 as the first condition in order to be able to collide 92 00:05:27.614 --> 00:05:30.544 The second condition, between objects 93 00:05:30.544 --> 00:05:32.030 there should be 94 00:05:32.030 --> 00:05:34.940 at least one of what? 95 00:05:34.940 --> 00:05:36.505 One should be able to move at least 96 00:05:36.505 --> 00:05:38.554 And that's the Rigidbody 97 00:05:38.554 --> 00:05:43.584 With this concept, let's work on object collision processing 98 00:05:46.921 --> 00:05:48.386 Now in Unity 99 00:05:48.386 --> 00:05:49.762 Let's go to unity editor 100 00:05:49.772 --> 00:05:52.693 Let's look at an Enemy first 101 00:05:52.693 --> 00:05:57.624 You can see a Box Collider attached 102 00:05:57.624 --> 00:05:59.198 In the Inspector window 103 00:05:59.198 --> 00:06:02.148 you can see the first condition 104 00:06:02.148 --> 00:06:04.228 collider is already attached 105 00:06:04.228 --> 00:06:07.762 but the Rigidbody is missing among components 106 00:06:07.762 --> 00:06:09.525 Let's look at the Player, too 107 00:06:09.525 --> 00:06:11.269 So, Player also has 108 00:06:11.269 --> 00:06:15.643 Box Collider by default 109 00:06:15.643 --> 00:06:18.614 However, there isn't any Rigidbody here as well 110 00:06:18.614 --> 00:06:22.257 Therefore, if we wanted to have this enemy 111 00:06:22.257 --> 00:06:23.891 collide with Player 112 00:06:23.891 --> 00:06:27.733 when we press play button, would they collide? 113 00:06:27.733 --> 00:06:28.634 No, they wouldn't 114 00:06:28.634 --> 00:06:33.218 Why? because the second condition, Rigidbody is missing 115 00:06:33.218 --> 00:06:38.000 Therefore, let's attach Rigidbody to this Enemy 116 00:06:38.000 --> 00:06:39.321 go to Add Component 117 00:06:39.321 --> 00:06:42.374 If you search Rigidbody here 118 00:06:49.307 --> 00:06:52.000 Rigidbody, add into the component like this 119 00:06:52.000 --> 00:06:55.208 Then you can see that the Rigidbody component is attached here 120 00:06:55.208 --> 00:06:57.396 Now this object can collide 121 00:06:57.396 --> 00:06:59.896 By default here, among properties 122 00:06:59.896 --> 00:07:02.426 things like Use Gravity are enabled 123 00:07:02.426 --> 00:07:05.129 So if we turn off the script for now 124 00:07:05.129 --> 00:07:07.179 if we drag it down 125 00:07:07.179 --> 00:07:09.713 and movement related components are disabled 126 00:07:09.713 --> 00:07:13.000 Unchecking this box means it will be disabled 127 00:07:13.000 --> 00:07:16.742 So the script here is not working 128 00:07:16.742 --> 00:07:20.802 If we set it like this, basic physics will be applied 129 00:07:20.802 --> 00:07:23.960 The physics function for movement will take effect 130 00:07:23.960 --> 00:07:27.861 Since Rigidbody will be under effect of gravity, it will fall 131 00:07:27.861 --> 00:07:28.871 Now let's see 132 00:07:28.871 --> 00:07:32.247 I will move this so they would align a bit 133 00:07:32.247 --> 00:07:34.733 Let's see what happens when this object falls 134 00:07:34.733 --> 00:07:36.406 Let's press the play button 135 00:07:36.406 --> 00:07:40.356 when we play it, it approaches the object 136 00:07:40.356 --> 00:07:44.297 and bounce off the object 137 00:07:44.297 --> 00:07:46.447 n the Unity engine, there is content 138 00:07:46.447 --> 00:07:50.000 that automatically applies physics like this 139 00:07:50.000 --> 00:07:53.000 From collision to physics phenomenon to occur 140 00:07:53.000 --> 00:07:56.050 For this to be possible, the first condition 141 00:07:56.050 --> 00:07:59.594 a Box Collider, or collider must be attached 142 00:07:59.594 --> 00:08:03.337 Both Player and Enemy need a collider attached 143 00:08:03.337 --> 00:08:05.987 And then, one of the objects 144 00:08:05.987 --> 00:08:07.792 must have what attached? 145 00:08:07.792 --> 00:08:08.635 Rigidbody 146 00:08:08.635 --> 00:08:12.000 Rigidbody must be attached as proof that it is a moving object 147 00:08:12.000 --> 00:08:13.863 This allows us to create the action 148 00:08:13.863 --> 00:08:17.762 where objects physically collide 149 00:08:17.762 --> 00:08:23.812 So, in our case, we check the Enemy again and open it 150 00:08:23.812 --> 00:08:26.312 and for Enemy and Player to be able to collide 151 00:08:26.312 --> 00:08:27.653 they need Rigidbody 152 00:08:27.653 --> 00:08:30.153 However, in Enemy Script 153 00:08:30.153 --> 00:08:32.000 it is moving Enemy 154 00:08:32.000 --> 00:08:35.250 We'll turn off the effects applied 155 00:08:35.250 --> 00:08:37.396 by Use Gravity 156 00:08:37.396 --> 00:08:39.596 The physics part here 157 00:08:39.596 --> 00:08:40.980 we will have it turned off 158 00:08:44.020 --> 00:08:48.820 Now then, we'll process the event 159 00:08:48.820 --> 00:08:50.832 that occurs when two objects collide 160 00:08:50.832 --> 00:08:53.982 In other words, we will implement 161 00:08:53.982 --> 00:08:58.950 how to handle the situation when two objects collide 162 00:08:58.950 --> 00:09:05.861 Now, in terms of collision, here we have the Enemy 163 00:09:05.861 --> 00:09:09.109 Objects are to collide with each other 164 00:09:09.109 --> 00:09:11.119 and we have Player here 165 00:09:11.119 --> 00:09:14.277 and then we have Bullet 166 00:09:14.277 --> 00:09:17.307 We've created these three objects 167 00:09:17.307 --> 00:09:20.207 Then, shall we take a look at how they collide 168 00:09:20.207 --> 00:09:21.436 with each other? 169 00:09:21.436 --> 00:09:26.000 First, can the Player and Enemy collide? 170 00:09:26.000 --> 00:09:29.792 Can these two objects collide with each other, or not? 171 00:09:29.792 --> 00:09:31.683 They can, ding! 172 00:09:31.683 --> 00:09:33.983 Then can Enemy and Bullet 173 00:09:33.983 --> 00:09:36.000 collide with each other, or not? 174 00:09:36.000 --> 00:09:38.139 They must be able to 175 00:09:38.139 --> 00:09:41.139 Only then, when Player and Enemy collide 176 00:09:41.139 --> 00:09:42.389 we can make Player die or 177 00:09:42.389 --> 00:09:43.990 Enemy die 178 00:09:43.990 --> 00:09:45.940 and also to hit Enemy with Bullets 179 00:09:45.940 --> 00:09:47.812 That's the logic behind this 180 00:09:47.812 --> 00:09:50.562 But Player and Bullet 181 00:09:50.562 --> 00:09:52.990 should they able to collide with each other? 182 00:09:52.990 --> 00:09:56.158 Of course, in reality, it would be natural for them to collide 183 00:09:56.158 --> 00:09:59.891 Also, depending on the design of the game, it’s possible to make them collide or not collide 184 00:09:59.891 --> 00:10:02.541 But in our game, we won't have situations 185 00:10:02.541 --> 00:10:05.491 where Player dying when hit by 186 00:10:05.491 --> 00:10:07.228 its own Bullet 187 00:10:07.228 --> 00:10:10.733 Therefore, we will forbid the collision between the two 188 00:10:10.733 --> 00:10:14.208 Then, we have a common denominator here 189 00:10:14.208 --> 00:10:17.258 When they collide, in the end, both the Enemy 190 00:10:17.258 --> 00:10:19.858 and either the Player or Bullet will be destroyed 191 00:10:19.858 --> 00:10:21.000 we will have them destroyed 192 00:10:21.000 --> 00:10:23.850 Then, ideally, each of them should 193 00:10:23.850 --> 00:10:28.152 handle this on their own, but 194 00:10:28.152 --> 00:10:31.752 In reality, doesn't matter which 195 00:10:31.752 --> 00:10:34.102 if one object handles the collision 196 00:10:34.102 --> 00:10:35.861 it will apply to the other objects as well 197 00:10:35.861 --> 00:10:39.661 Then, the common denominator 198 00:10:39.661 --> 00:10:41.129 would be the Enemy part here 199 00:10:41.129 --> 00:10:44.779 So, let's add the logic in the Enemy part 200 00:10:44.779 --> 00:10:46.979 to remove both the object 201 00:10:46.979 --> 00:10:50.752 that collided with it and itself 202 00:10:50.752 --> 00:10:53.402 So, let's go to the script of Enemy 203 00:10:53.402 --> 00:10:56.000 Let's go to the Enemy script 204 00:10:59.148 --> 00:11:01.148 In the visual studio 205 00:11:01.148 --> 00:11:04.663 come to Enemy.cs file 206 00:11:04.663 --> 00:11:08.148 What we want to do here is 207 00:11:08.148 --> 00:11:15.373 When Enemy here collide with other object 208 00:11:17.848 --> 00:11:26.346 Let's add the logic to say, I want both you and me to die when we collide 209 00:11:26.346 --> 00:11:28.280 then it means to destroy both the object 210 00:11:28.280 --> 00:11:30.842 that collided with this object and itself 211 00:11:30.842 --> 00:11:34.148 So, how do we handle this part then? 212 00:11:34.148 --> 00:11:38.653 the lifecycle function for spawning is Start 213 00:11:38.653 --> 00:11:40.634 and the function for living is Update 214 00:11:40.634 --> 00:11:43.663 Then for dying? It's OnDestroy 215 00:11:43.663 --> 00:11:45.208 Why do we have the On there? 216 00:11:45.208 --> 00:11:49.932 The 'On' prefix is a convention in Unity used for functions 217 00:11:49.932 --> 00:11:52.535 that are triggered by events 218 00:11:52.535 --> 00:11:55.435 Similarly, we can't know 219 00:11:55.435 --> 00:11:57.335 when this object will collide 220 00:11:57.335 --> 00:11:58.792 with another object 221 00:11:58.792 --> 00:12:02.442 That's why we use prefixes like On for such functions 222 00:12:02.442 --> 00:12:06.192 So, when we look at OnCollisionEnter 223 00:12:06.192 --> 00:12:08.950 there are Enter, Exit 224 00:12:08.950 --> 00:12:10.260 and Stay 225 00:12:10.260 --> 00:12:13.760 When making a 2D game, you just need to add 2D at the end 226 00:12:13.760 --> 00:12:16.460 Now, OnCollisionEnter refers to 227 00:12:16.460 --> 00:12:19.260 the exact moment when a collision happens 228 00:12:19.260 --> 00:12:23.160 Exit refers to the moment when the collision happens 229 00:12:23.160 --> 00:12:26.820 Stay refers to the state where the objects remain in collision 230 00:12:26.820 --> 00:12:30.670 So, when an object collides with another 231 00:12:30.670 --> 00:12:33.419 you can use OnCollisionEnter right at that moment 232 00:12:33.419 --> 00:12:38.099 By doing this, you'll have void OnCollisionEnter 233 00:12:38.099 --> 00:12:40.849 I will set this Collision 234 00:12:40.849 --> 00:12:42.749 the name of this variable parameter 235 00:12:42.749 --> 00:12:44.700 as other for the Collision 236 00:12:44.700 --> 00:12:48.850 Then, it will become the name of the object that collided with me 237 00:12:48.850 --> 00:12:51.060 So say, You will be destroyed, I will be destroyed 238 00:12:51.060 --> 00:12:54.716 Let's implement this part 239 00:12:54.716 --> 00:12:56.743 and add it here 240 00:13:00.259 --> 00:13:04.331 So, for You will be destroyed 241 00:13:04.331 --> 00:13:07.059 we can use the Destroy function 242 00:13:07.059 --> 00:13:09.659 Instead of just destroying other 243 00:13:09.659 --> 00:13:12.580 if you look here, this Collision object comes in 244 00:13:12.580 --> 00:13:15.680 What I want to destroy is 245 00:13:15.680 --> 00:13:18.459 the other object's game object 246 00:13:18.459 --> 00:13:22.540 So, if you look at other, there is gameObject 247 00:13:22.540 --> 00:13:25.650 why? you are not trying to destroy just parts of an object 248 00:13:25.650 --> 00:13:27.820 then what do you want to destroy? 249 00:13:27.820 --> 00:13:31.670 We need to destroy the game object itself, including everything 250 00:13:31.670 --> 00:13:33.820 in order to destroy our object 251 00:13:33.820 --> 00:13:36.470 So if we set up like this, we achieve, you will be destroyed 252 00:13:36.470 --> 00:13:40.580 and then for I will be destroyed 253 00:13:40.580 --> 00:13:43.430 If we use just gameObject without other 254 00:13:43.430 --> 00:13:46.059 then it becomes I 255 00:13:46.059 --> 00:13:48.909 There is gameObject that 256 00:13:48.909 --> 00:13:52.712 this Enemy.cs component 257 00:13:52.712 --> 00:13:55.627 we destroy it by using Destroy(gameObject) 258 00:13:55.627 --> 00:13:58.686 Shall we check what happens when colliding with other objects? 259 00:14:04.388 --> 00:14:10.418 In Unity, press play, we can see both objects get destroyed 260 00:14:10.418 --> 00:14:13.916 When it collides with Player, it gets destroyed 261 00:14:13.916 --> 00:14:18.803 Bang, bang, bang! When we fire Bullets, it can also destroy like this 262 00:14:22.308 --> 00:14:24.308 So, we can see how processing of the collision 263 00:14:24.308 --> 00:14:25.902 between two objects are like 264 00:14:30.720 --> 00:14:33.970 Now let's learn about 265 00:14:33.970 --> 00:14:35.320 enemy auto generation 266 00:14:35.320 --> 00:14:38.020 Right now we are using simply one enemy 267 00:14:38.020 --> 00:14:39.240 like this 268 00:14:39.240 --> 00:14:41.140 But the goal of the game wouldn't be 269 00:14:41.140 --> 00:14:42.560 to destroy only this one enemy 270 00:14:42.560 --> 00:14:44.910 For multiple enemies to spawn 271 00:14:44.910 --> 00:14:47.680 let's create multiple enemies 272 00:14:47.680 --> 00:14:51.497 We already know the theoretical concept here 273 00:14:51.497 --> 00:14:52.597 Bullets are 274 00:14:52.597 --> 00:14:55.719 created in Bullet Factory 275 00:14:55.719 --> 00:14:58.379 Then, where are Enemies created? 276 00:14:58.379 --> 00:15:00.959 We can create them from an enemy factory 277 00:15:00.959 --> 00:15:03.759 If there is a drop item in the world, for example 278 00:15:03.759 --> 00:15:07.240 Where should they be created? 279 00:15:07.240 --> 00:15:10.639 Also, we can create them from an item factory 280 00:15:10.639 --> 00:15:12.439 It's the same method for everything 281 00:15:12.439 --> 00:15:14.139 The only difference is the name 282 00:15:14.139 --> 00:15:16.320 you can create them all using the same method 283 00:15:16.320 --> 00:15:19.160 So, we need the Enemy factory for Enemies 284 00:15:19.160 --> 00:15:21.320 Remember how to make a factory? 285 00:15:21.320 --> 00:15:23.520 We called this factory prefabs 286 00:15:23.520 --> 00:15:25.920 A prefab is called a prefab when 287 00:15:25.920 --> 00:15:29.599 a gameObject becomes an asset file 288 00:15:29.599 --> 00:15:31.163 Then we can make this gameObject 289 00:15:31.163 --> 00:15:33.480 the Enemy gameObject into a file 290 00:15:33.480 --> 00:15:34.980 drag and drop it 291 00:15:34.980 --> 00:15:37.030 into the project window that manages files 292 00:15:37.030 --> 00:15:38.720 if you drag and drop 293 00:15:38.720 --> 00:15:41.519 it will be added into this window like this 294 00:15:41.519 --> 00:15:44.760 Then we don't need the Enemy here 295 00:15:44.760 --> 00:15:47.510 We will have Enemies created 296 00:15:47.510 --> 00:15:50.120 one by one whenever we need one 297 00:15:50.120 --> 00:15:52.559 Let's erase an Enemy here 298 00:15:52.559 --> 00:15:55.639 Then, who will create this Enemy when it's needed? 299 00:15:55.639 --> 00:15:57.139 Bullets are made by a Player 300 00:15:57.139 --> 00:15:59.519 Whenever we press fire button 301 00:15:59.519 --> 00:16:02.480 We need someone creating Enemies for us 302 00:16:02.480 --> 00:16:06.130 So, the object responsible for that role 303 00:16:06.130 --> 00:16:07.780 We will call it the Enemy Manager 304 00:16:09.879 --> 00:16:13.029 So, in Hierarchy window 305 00:16:13.029 --> 00:16:15.639 press plus button and Create Empty 306 00:16:15.639 --> 00:16:17.589 Enemy Manager is as the name suggests 307 00:16:17.589 --> 00:16:19.599 we use it just for management purposes 308 00:16:19.599 --> 00:16:20.919 We don't need it to be visible 309 00:16:20.919 --> 00:16:22.440 Just like God 310 00:16:22.440 --> 00:16:25.440 So by pressing Create Empty button 311 00:16:25.440 --> 00:16:28.120 we can make it invisible 312 00:16:28.120 --> 00:16:33.839 and name it EnemyManager 313 00:16:33.839 --> 00:16:36.519 For Transform, Z value is set to minus 10 314 00:16:36.519 --> 00:16:38.362 press reset button to set them to 0, 0, 0 315 00:16:41.124 --> 00:16:45.360 We'll create it like this and place it at the top 316 00:16:45.360 --> 00:16:46.919 but it won't be easily visible, right? 317 00:16:46.919 --> 00:16:51.279 So, for invisible empty objects like this 318 00:16:51.279 --> 00:16:54.429 if you look on the right side 319 00:16:54.429 --> 00:16:56.919 there's the Inspector window 320 00:16:56.919 --> 00:17:00.279 This part in the Inspector window 321 00:17:00.279 --> 00:17:02.429 If you open it 322 00:17:02.429 --> 00:17:06.360 you can select it as an icon here instead 323 00:17:06.360 --> 00:17:07.839 I'll just choose any icon 324 00:17:07.839 --> 00:17:12.714 something like a diamond shape 325 00:17:12.714 --> 00:17:16.399 As you can see, the diamond icon is assigned here 326 00:17:16.399 --> 00:17:20.119 and we can actually see the diamond icon with our eyes as well 327 00:17:20.119 --> 00:17:22.669 But in a real screen in game 328 00:17:22.669 --> 00:17:25.000 and we can actually see the diamond icon with our eyes as well 329 00:17:25.000 --> 00:17:26.800 So, what you can do is 330 00:17:26.800 --> 00:17:28.750 just select the Gizmo 331 00:17:28.750 --> 00:17:32.440 and you'll see the Gizmo in the game view 332 00:17:32.440 --> 00:17:34.919 if you press this button 333 00:17:34.919 --> 00:17:39.000 We can make the Gizmos, which we previously saw in the Scene view 334 00:17:39.000 --> 00:17:41.639 visible in the Game view as well 335 00:17:41.639 --> 00:17:43.520 Then, when it appears in the Game view 336 00:17:43.520 --> 00:17:45.039 what do we do next? 337 00:17:45.039 --> 00:17:48.279 If we make the Enemy appear here 338 00:17:48.279 --> 00:17:49.779 the Enemy will suddenly 339 00:17:49.779 --> 00:17:51.679 pop up at this location 340 00:17:51.679 --> 00:17:52.800 Little awkward, right? 341 00:17:52.800 --> 00:17:55.650 It would be better if it were created outside the screen 342 00:17:55.650 --> 00:17:58.479 and then came in, making it look more natural 343 00:17:58.479 --> 00:18:00.720 So, to make it leave the screen 344 00:18:00.720 --> 00:18:04.479 I'll place it like this and move it upwards 345 00:18:04.479 --> 00:18:07.119 Now, it's positioned outside the screen 346 00:18:07.119 --> 00:18:10.160 And we can see this by selecting the Main Camera 347 00:18:10.160 --> 00:18:12.810 he area of the Main Camera is 348 00:18:12.810 --> 00:18:15.080 displayed as a white box 349 00:18:15.080 --> 00:18:18.720 You can see this in the preview window here 350 00:18:18.720 --> 00:18:23.080 Right now, the position of the EnemyManager we are creating 351 00:18:23.080 --> 00:18:25.759 is outside the Main Camera's area 352 00:18:25.759 --> 00:18:29.360 Then, we can tell that it's placed outside the screen 353 00:18:29.360 --> 00:18:31.610 Let's handle it so that the Enemy is created outside 354 00:18:31.610 --> 00:18:34.457 and then comes into the screen 355 00:18:37.318 --> 00:18:39.880 Let's create the EnemyManager 356 00:18:39.880 --> 00:18:41.880 and then make a script 357 00:18:41.880 --> 00:18:42.960 to handle the tasks 358 00:18:42.960 --> 00:18:48.040 in the Scripts folder, press plus button for C# 359 00:18:48.040 --> 00:18:51.399 Let's name it EnemyManager 360 00:18:56.200 --> 00:18:57.750 EnemyManager 361 00:18:57.750 --> 00:19:02.559 In the gameObject, place EnemyManager C# script 362 00:19:02.559 --> 00:19:04.320 The name doesn't have to be exactly the same, though 363 00:19:04.320 --> 00:19:06.839 I've named them this way for convenience 364 00:19:06.839 --> 00:19:10.200 just to make it easier to recognize the game object 365 00:19:10.200 --> 00:19:13.119 and the script 366 00:19:13.119 --> 00:19:15.880 Now, let's open the EnemyManager script 367 00:19:15.880 --> 00:19:19.079 if you look, the script names are all cut off right now 368 00:19:19.079 --> 00:19:20.480 Beyond the size 369 00:19:20.480 --> 00:19:23.799 you'll see a scrollbar at the bottom here 370 00:19:23.799 --> 00:19:24.871 in the Unity editor 371 00:19:24.871 --> 00:19:27.839 If you grab the scrollbar and move it all the way to the left 372 00:19:27.839 --> 00:19:31.320 the full names will be displayed correctly 373 00:19:31.320 --> 00:19:34.040 We are adjusting the preview size 374 00:19:34.040 --> 00:19:36.490 Here, we will double-click 375 00:19:36.490 --> 00:19:39.577 on EnemyManager to open it 376 00:19:39.577 --> 00:19:41.760 what we will do in the EnemyManager 377 00:19:41.760 --> 00:19:44.040 is this 378 00:19:44.040 --> 00:19:49.200 Want to create Ememies at regular intervals 379 00:19:49.200 --> 00:19:54.399 This is what we will use EnemyManager for 380 00:19:54.399 --> 00:19:57.557 So, what properties are needed here? 381 00:20:01.359 --> 00:20:03.009 To implement that sentence 382 00:20:03.009 --> 00:20:04.880 what information do we need 383 00:20:04.880 --> 00:20:05.780 to get from the designer? 384 00:20:05.780 --> 00:20:08.839 We'll need a factory that can create enemies 385 00:20:08.839 --> 00:20:12.089 and if you look here 386 00:20:12.089 --> 00:20:15.160 We need to know how long the interval is 387 00:20:15.160 --> 00:20:18.119 The regular interval refers to the spawn time 388 00:20:18.119 --> 00:20:22.640 So, we need to get the spawn time information from the designer 389 00:20:22.640 --> 00:20:25.279 And although it's hidden in this sentence 390 00:20:25.279 --> 00:20:26.880 we also need the elapsed time 391 00:20:26.880 --> 00:20:30.119 To know how long has passed 392 00:20:30.119 --> 00:20:33.320 Since we need these three properties for this reason 393 00:20:33.320 --> 00:20:36.213 Let's declare these three properties 394 00:20:38.480 --> 00:20:41.679 Shall we look at Enemy factory now? 395 00:20:41.679 --> 00:20:52.640 we will use public GameObject enemyFactory 396 00:20:52.640 --> 00:20:55.559 since the spawn time needs to be editable by the designer 397 00:20:55.559 --> 00:20:58.239 making it public will expose it to the outside 398 00:20:58.239 --> 00:21:01.760 Next, let's add float createTime like so 399 00:21:01.760 --> 00:21:04.160 and set 2 seconds for the amount of time 400 00:21:04.160 --> 00:21:07.279 so that Enemies are created every 2 seconds 401 00:21:07.279 --> 00:21:10.079 Next, we need the elapsed time 402 00:21:10.079 --> 00:21:13.279 The name of the elapsed time doesn't need to be exposed to the outside 403 00:21:13.279 --> 00:21:15.000 The designer doesn't have to define it 404 00:21:15.000 --> 00:21:16.920 Why so? It's because time continues to flow 405 00:21:16.920 --> 00:21:19.239 I'll name it currentTime 406 00:21:19.239 --> 00:21:23.000 since it flows according to current time 407 00:21:23.000 --> 00:21:27.480 So, we have declared the three necessary properties like this 408 00:21:27.480 --> 00:21:29.817 Now, our ultimate goal is 409 00:21:29.817 --> 00:21:32.040 to create enemies at regular intervals 410 00:21:32.040 --> 00:21:35.079 Where should this be implemented? 411 00:21:35.079 --> 00:21:36.979 You can Update 412 00:21:36.979 --> 00:21:38.720 From Start or Update 413 00:21:38.720 --> 00:21:41.160 We will have it keep creating 414 00:21:41.160 --> 00:21:45.160 What we ultimately want to 415 00:21:45.160 --> 00:21:47.480 achieve at this point is 416 00:21:47.480 --> 00:21:50.430 Wanting to create an enemy 417 00:21:50.430 --> 00:21:53.829 that is our ultimate goal 418 00:21:53.829 --> 00:21:57.559 The second thing is, we ultimately want to create the enemy 419 00:21:57.559 --> 00:21:59.279 Now we need to ask some questions 420 00:21:59.279 --> 00:22:01.239 Why do we make Enemies so suddenly? 421 00:22:01.239 --> 00:22:06.880 Since the specified time has passed, or since the spawn time has arrived 422 00:22:06.880 --> 00:22:12.200 That's why we want the Enemies to be created 423 00:22:12.200 --> 00:22:14.440 Up to this point, this is something you might 424 00:22:14.440 --> 00:22:17.279 generally consider when coding 425 00:22:17.279 --> 00:22:21.239 However, for those who are not familiar with coding 426 00:22:21.239 --> 00:22:23.880 they might only come up with designs only to this point 427 00:22:23.880 --> 00:22:26.480 However, there is a hidden parts 428 00:22:26.480 --> 00:22:28.079 the logical aspect that is hidden 429 00:22:28.079 --> 00:22:30.480 specific to computer languages 430 00:22:30.480 --> 00:22:32.959 What it is, is this, creating Enemy 431 00:22:32.959 --> 00:22:34.720 then it needs to reach the spawn time 432 00:22:34.720 --> 00:22:36.770 Then lastly, we need one more sentence 433 00:22:36.770 --> 00:22:38.799 to be formed 434 00:22:38.799 --> 00:22:40.519 to complete this entire section 435 00:22:40.519 --> 00:22:44.359 Time needs to flow 436 00:22:44.359 --> 00:22:46.559 It may seem obvious, but this part is 437 00:22:46.559 --> 00:22:48.679 actually very important in programming 438 00:22:48.679 --> 00:22:51.160 It's very common to overlook these things 439 00:22:51.160 --> 00:22:55.079 These conditions need to be met for the time to pass 440 00:22:55.079 --> 00:22:56.679 and for the spawn time to be reached 441 00:22:56.679 --> 00:23:01.200 which completes the three logic of creating enemies 442 00:23:01.200 --> 00:23:04.559 So, the first thing we ultimately want to do is 443 00:23:04.559 --> 00:23:07.978 following this flow, which is once the time passes 444 00:23:07.978 --> 00:23:11.039 and the spawn time is reached, we create the enemy 445 00:23:11.039 --> 00:23:14.919 Now, let's implement the part where time passes 446 00:23:14.919 --> 00:23:17.599 By time passing, we mean the elapsed time 447 00:23:17.599 --> 00:23:23.359 When curretTime flows, it means the time is accumulating 448 00:23:23.359 --> 00:23:26.520 currentTime will flow like this 449 00:23:26.520 --> 00:23:29.640 but we can reduce it like this 450 00:23:29.640 --> 00:23:30.940 This accumulates 451 00:23:30.940 --> 00:23:33.479 but about time, we learned this one thing 452 00:23:33.479 --> 00:23:37.239 What is being used when the Enemy is moving? 453 00:23:37.239 --> 00:23:41.159 deltaTime, what is it about? 454 00:23:41.159 --> 00:23:42.909 The interval between frames 455 00:23:42.909 --> 00:23:45.799 The time it takes to draw a picture on the screen 456 00:23:45.799 --> 00:23:47.142 or this Update here 457 00:23:47.142 --> 00:23:49.679 The time it takes for Update to draw a picture on the screen 458 00:23:49.679 --> 00:23:52.400 I said Update is done once per frame 459 00:23:52.400 --> 00:23:54.743 Therefore, you can think of it as 460 00:23:54.743 --> 00:23:58.159 the time it takes for an Update to be called 461 00:23:58.159 --> 00:23:59.320 Then what is going on? 462 00:23:59.320 --> 00:24:01.370 Time taken for a single update call 463 00:24:01.370 --> 00:24:03.400 Time taken to process a single update call 464 00:24:03.400 --> 00:24:05.679 the interval should be very short 465 00:24:05.679 --> 00:24:09.280 So, how about we continuously accumulate this time? 466 00:24:09.280 --> 00:24:12.380 If we keep accumulating 0.03 seconds and 0.01 seconds 467 00:24:12.380 --> 00:24:15.060 eventually it will add up to 1 second 468 00:24:15.060 --> 00:24:16.119 So this is what happens 469 00:24:16.119 --> 00:24:18.919 If we keep adding 1/30th of a second 470 00:24:18.919 --> 00:24:24.269 it means we're aiming to match 471 00:24:24.269 --> 00:24:25.559 30 frames per second 472 00:24:25.559 --> 00:24:27.599 So when this is activated 30 times 473 00:24:27.599 --> 00:24:28.842 currentTime will become 1 second 474 00:24:28.842 --> 00:24:32.520 That's the magic we are trying to make happen 475 00:24:32.520 --> 00:24:34.960 And then, the Generation time came means 476 00:24:34.960 --> 00:24:39.634 If we slightly modify this into coding language 477 00:24:39.634 --> 00:24:40.400 it will look like this 478 00:24:40.400 --> 00:24:45.799 If the elapsed time exceeds the generation time 479 00:24:45.799 --> 00:24:49.066 It’s the same content, but we could rephrase the sentence 480 00:24:52.080 --> 00:24:54.479 So we use if for this sentence 481 00:24:54.479 --> 00:24:58.239 the elapsed time, currentTime is bigger than what? 482 00:24:58.239 --> 00:25:01.599 if it's bigger than createTime 483 00:25:01.599 --> 00:25:03.520 If it exceeds, it means it's greater 484 00:25:03.520 --> 00:25:05.599 That is, it's a simple translation 485 00:25:05.599 --> 00:25:09.960 If the elapsed time exceeds 486 00:25:09.960 --> 00:25:11.840 what? the createTime 487 00:25:11.840 --> 00:25:14.880 You can create it like this with a simple translation 488 00:25:14.880 --> 00:25:16.280 What do you want to do at this point? 489 00:25:16.280 --> 00:25:20.359 We want to make the Enemy 490 00:25:20.359 --> 00:25:25.799 To create the object, we can simply produce it at the object factory 491 00:25:25.799 --> 00:25:29.159 What is this? It’s called instantiate 492 00:25:29.159 --> 00:25:30.479 You've used it when creating Bullet object 493 00:25:30.479 --> 00:25:33.320 If we place EnemyFactory here 494 00:25:33.320 --> 00:25:36.400 we can immediately create Enemy 495 00:25:36.400 --> 00:25:41.200 and the gameObjects created here 496 00:25:41.200 --> 00:25:43.700 GameObject enemy 497 00:25:43.700 --> 00:25:46.458 Like this, we can store this instance 498 00:25:46.458 --> 00:25:48.599 into variables to use them 499 00:25:48.599 --> 00:25:51.320 Now, when we place the Factory 500 00:25:51.320 --> 00:25:53.599 Like this, an instance is created 501 00:25:53.599 --> 00:25:55.960 What are we doing here at the end? 502 00:25:55.960 --> 00:26:00.039 We place Enemies 503 00:26:00.039 --> 00:26:03.400 Then, the position of the enemy's transform 504 00:26:03.400 --> 00:26:05.250 enemy and a dot 505 00:26:05.250 --> 00:26:08.159 We're accessing the transform among the properties or functions 506 00:26:08.159 --> 00:26:09.809 transform and a dot 507 00:26:09.809 --> 00:26:12.479 Among your properties, give me your position 508 00:26:12.479 --> 00:26:15.119 To place it means inserting the position value 509 00:26:15.119 --> 00:26:20.599 I will insert the position of the EnemyManager here 510 00:26:20.599 --> 00:26:22.119 Use transform dot position 511 00:26:22.119 --> 00:26:26.119 It's own location goes in 512 00:26:26.119 --> 00:26:28.400 We are not finished with this sentence yet 513 00:26:28.400 --> 00:26:32.080 In this sentence, it happens once every certain amount of time 514 00:26:32.080 --> 00:26:35.200 After creating it once, the currentTime keeps accumulating 515 00:26:35.200 --> 00:26:36.880 so once it exceeds CreateTime 516 00:26:36.880 --> 00:26:38.599 it will keep increasing 517 00:26:38.599 --> 00:26:41.960 and be created continuously without waiting 518 00:26:41.960 --> 00:26:44.200 After creating it once, you would need to wait again 519 00:26:44.200 --> 00:26:46.880 Need to wait, then create it again, wait again, and then create it again 520 00:26:46.880 --> 00:26:48.520 This process should repeat 521 00:26:48.520 --> 00:26:53.119 So, a process to reset the elapsed time is necessary 522 00:26:53.119 --> 00:26:57.648 So, as the final step, you need to reset the elapsed time 523 00:27:02.400 --> 00:27:05.200 If you reset currentTime to 0 524 00:27:05.200 --> 00:27:08.919 the timer will be reset to 0 and start counting up again 525 00:27:08.919 --> 00:27:14.919 Once saved like this, let's go to Unity 526 00:27:14.919 --> 00:27:18.200 When you come here, you’ll see that in Unity 527 00:27:18.200 --> 00:27:21.000 the Enemy Manager is now there 528 00:27:21.000 --> 00:27:23.200 Here, the Create Time is set to 2 529 00:27:23.200 --> 00:27:26.960 I will drag and drop the Enemy from Prefabs 530 00:27:26.960 --> 00:27:29.520 into the Enemy Factory section 531 00:27:29.520 --> 00:27:31.280 EnemyFactory 532 00:27:31.280 --> 00:27:33.602 Shall we press the play button now? 533 00:27:40.479 --> 00:27:41.919 If we let it play 534 00:27:41.919 --> 00:27:45.159 You will be able to notice that a problem has occurred 535 00:27:45.159 --> 00:27:48.942 The reason for this is that we need to look at the console window here 536 00:27:52.080 --> 00:27:57.072 You can see an UnassignedReferenceException here 537 00:27:57.072 --> 00:28:00.239 In the console window, the error message is displayed in red 538 00:28:00.239 --> 00:28:03.200 It says 'Unassigned', meaning it hasn't been assigned 539 00:28:03.200 --> 00:28:05.880 And then if you look here 540 00:28:05.880 --> 00:28:10.559 The target variable of the Enemy has not been assigned any value 541 00:28:10.559 --> 00:28:12.640 that is what we have here 542 00:28:12.640 --> 00:28:16.000 So, if you double-click on it 543 00:28:16.000 --> 00:28:17.479 to see where the problem is 544 00:28:17.479 --> 00:28:20.840 by clicking on it in the console window 545 00:28:20.840 --> 00:28:23.799 it will show that this part is not assigned 546 00:28:23.799 --> 00:28:25.520 meaning it has no value 547 00:28:25.520 --> 00:28:28.919 It clearly shows here that the target value is missing 548 00:28:28.919 --> 00:28:31.440 But we did insert target right there? 549 00:28:31.440 --> 00:28:32.940 The target value 550 00:28:32.940 --> 00:28:34.320 You can think of it this way 551 00:28:34.320 --> 00:28:38.159 But there is no Enemy here 552 00:28:38.159 --> 00:28:40.799 If you go to the Enemy and check 553 00:28:40.799 --> 00:28:43.973 does the target have a value? No, it doesn't 554 00:28:43.973 --> 00:28:46.479 Ah, I see. So, I need to assign the player again 555 00:28:46.479 --> 00:28:47.960 You can think of it like that 556 00:28:47.960 --> 00:28:51.200 Shall we try? I tried to put the Player here 557 00:28:51.200 --> 00:28:52.880 but does it go in or not? 558 00:28:52.880 --> 00:28:55.159 It doesn't go in and the reason is that 559 00:28:55.159 --> 00:28:59.479 these are instances, which are 560 00:28:59.479 --> 00:29:01.280 in memory right now 561 00:29:01.280 --> 00:29:04.370 They disappear when you turn off the computer, in short 562 00:29:04.370 --> 00:29:07.359 Only the things that are here get stored into the hard drive 563 00:29:07.359 --> 00:29:10.559 So, the physical space is different 564 00:29:10.559 --> 00:29:13.039 That's why you can't put it here 565 00:29:13.039 --> 00:29:16.880 So, how do you assign a value to the target? 566 00:29:16.880 --> 00:29:17.960 One could ask 567 00:29:17.960 --> 00:29:20.479 So, you need to find it dynamically and assign the value 568 00:29:20.479 --> 00:29:23.159 By dynamic, it is when you press the play button 569 00:29:23.159 --> 00:29:26.509 the moment the game is running, the runtime moment 570 00:29:26.509 --> 00:29:28.039 we call it dynamic 571 00:29:28.039 --> 00:29:31.960 So, instead of directly assigning the target here 572 00:29:31.960 --> 00:29:34.599 what should we do 573 00:29:34.599 --> 00:29:37.400 right before it starts living just before it is created? 574 00:29:37.400 --> 00:29:43.719 We should let it find the target 575 00:29:43.719 --> 00:29:45.679 Who's the target then? 576 00:29:45.679 --> 00:29:50.200 It would be Player in this world 577 00:29:50.200 --> 00:29:52.150 If you have pressed the play button 578 00:29:52.150 --> 00:29:54.520 Then Enemy will spawn 579 00:29:54.520 --> 00:29:56.920 Since the EnemyManager will create the Enemy 580 00:29:56.920 --> 00:29:59.931 Then, at that moment, is the Player there in that space or not? 581 00:29:59.931 --> 00:30:04.802 Yes, because both are alive in the same space at that point 582 00:30:04.802 --> 00:30:08.800 Therefore, you can find it here 583 00:30:08.800 --> 00:30:12.850 So, this part where you need to find the target 584 00:30:12.850 --> 00:30:13.920 can be placed like this 585 00:30:13.920 --> 00:30:16.220 Currently, does the target have a value? No, it doesn't 586 00:30:16.220 --> 00:30:20.879 So, since it's an empty value, null is entered here 587 00:30:20.879 --> 00:30:24.129 Let's find a value and assign it to this null part 588 00:30:24.129 --> 00:30:25.920 How do we find it? Look closely 589 00:30:25.920 --> 00:30:30.470 Which property or function of GameObject dot? 590 00:30:30.470 --> 00:30:33.759 we use a function called Find 591 00:30:33.759 --> 00:30:36.359 It says string name right there 592 00:30:36.359 --> 00:30:38.409 It is the name of the GameObject 593 00:30:38.409 --> 00:30:41.470 What was the name? Player 594 00:30:41.470 --> 00:30:42.970 We find Player 595 00:30:42.970 --> 00:30:46.200 Then what role does Find play? 596 00:30:46.200 --> 00:30:47.700 It will find and pass 597 00:30:47.700 --> 00:30:49.900 the GameObject named Player 598 00:30:49.900 --> 00:30:52.080 if it exists in the world 599 00:30:52.080 --> 00:30:53.030 as what type? 600 00:30:53.030 --> 00:30:56.861 Since it's a GameObject, it will pass the GameObject 601 00:30:56.861 --> 00:31:01.400 But here, if it's found, we should pass the GameObject 602 00:31:01.400 --> 00:31:04.400 but what is its data type? It's Transform 603 00:31:04.400 --> 00:31:07.919 It won't work because we are trying to put a GameObject into a Transform 604 00:31:07.919 --> 00:31:11.377 So, let's first process it before proceeding 605 00:31:11.377 --> 00:31:16.320 Let's input player in lowercase for the GameObject 606 00:31:16.320 --> 00:31:18.660 Then, there should be no issue at all, right? 607 00:31:20.541 --> 00:31:24.031 Then, what if the Player dies? 608 00:31:24.031 --> 00:31:25.635 when colliding with an Enemy? 609 00:31:25.635 --> 00:31:28.713 When there is a Player 610 00:31:28.713 --> 00:31:32.959 when it is what? put a value into the target 611 00:31:32.959 --> 00:31:36.440 or it shouldn't have any values 612 00:31:36.440 --> 00:31:43.219 put a value into the target 613 00:31:43.219 --> 00:31:47.669 Therefore, if Player is there 614 00:31:47.669 --> 00:31:49.360 minus null, you could do this 615 00:31:49.360 --> 00:31:52.199 or put it in like this 616 00:31:52.199 --> 00:31:55.149 Here, you can put a value into the target 617 00:31:55.149 --> 00:31:58.720 player dot transform 618 00:31:58.720 --> 00:32:02.337 we can insert transform into player 619 00:32:02.337 --> 00:32:04.187 Then there wouldn't be any problems 620 00:32:04.187 --> 00:32:07.000 But then, what if Player dies? 621 00:32:07.000 --> 00:32:10.350 Then what would happen? 622 00:32:10.350 --> 00:32:12.000 There won't be any values inside the target 623 00:32:12.000 --> 00:32:14.350 So, we need to calculate this probability 624 00:32:14.350 --> 00:32:17.279 and at this point we need to handle one more thing 625 00:32:17.279 --> 00:32:23.557 By adding to the probability, there is a target 626 00:32:23.557 --> 00:32:24.602 Right? it's there 627 00:32:24.602 --> 00:32:32.199 And if the probability falls within 30% 628 00:32:32.199 --> 00:32:34.499 then let's move toward the target direction 629 00:32:34.499 --> 00:32:36.639 This is how it works, right? You understand? 630 00:32:36.639 --> 00:32:39.525 So, the project plan needs to be 631 00:32:39.525 --> 00:32:40.967 a bit more detailed 632 00:32:40.967 --> 00:32:46.240 How do we know there is a target? 633 00:32:46.240 --> 00:32:53.351 Just like before, we have the target, and then we can add ampersand 634 00:32:53.351 --> 00:32:57.039 You can insert the relational operator like this 635 00:32:57.039 --> 00:32:59.889 So, if there is a target and 636 00:32:59.889 --> 00:33:02.639 the percent falls within 30% 637 00:33:02.639 --> 00:33:04.039 then where does it go if there is no target? 638 00:33:04.039 --> 00:33:05.940 else, it just goes down 639 00:33:09.217 --> 00:33:11.919 Let's go and check it like this 640 00:33:11.919 --> 00:33:13.327 Now when we press the play button 641 00:33:18.291 --> 00:33:20.441 It comes over and then goes down 642 00:33:20.441 --> 00:33:23.594 You can see that an enemy comes every 2 seconds 643 00:33:27.981 --> 00:33:31.386 You can confirm that it continues to appear properly 644 00:33:41.029 --> 00:33:43.720 Shall we increase the probability? 645 00:33:43.720 --> 00:33:47.470 In ours, it's this Player 646 00:33:47.470 --> 00:33:52.440 So we made it to find the target when player is present 647 00:33:52.440 --> 00:33:55.890 Then I will set the probability 648 00:33:55.890 --> 00:33:59.253 as 10 to see what happens 649 00:33:59.253 --> 00:34:02.412 Let's save it and see what we've got 650 00:34:06.487 --> 00:34:09.875 Then it will come to the player unconditionally 651 00:34:11.479 --> 00:34:15.504 Right? Towards the player 652 00:34:15.504 --> 00:34:17.704 You can see that it's working properly 653 00:34:17.704 --> 00:34:20.004 Let's change percent value back to 3 now 654 00:34:20.004 --> 00:34:21.320 and get back to work 655 00:34:21.320 --> 00:34:22.946 if Player ceases to exist 656 00:34:25.040 --> 00:34:29.359 Enemy will, there, it died 657 00:34:29.359 --> 00:34:32.359 From then on, it will continue to move down 658 00:34:32.359 --> 00:34:33.839 Because there's no target 659 00:34:33.839 --> 00:34:37.939 Then, you can see that enemies were 660 00:34:37.939 --> 00:34:39.760 automatically respawning 661 00:34:39.760 --> 00:34:41.560 Like this, there is an EnemyManager 662 00:34:41.560 --> 00:34:43.360 Let's have multiple EnemyManagers 663 00:34:43.360 --> 00:34:45.597 not just one EnemyManager 664 00:34:45.597 --> 00:34:49.359 Let's press Ctrl and D on the keyboard 665 00:34:49.359 --> 00:34:52.909 How? press Ctrl key 666 00:34:52.909 --> 00:34:55.799 and then D at the same time to duplicate 667 00:34:55.799 --> 00:34:58.499 You will see it being duplicated 668 00:34:58.499 --> 00:35:00.921 So, just simply select EnemyManager 669 00:35:00.921 --> 00:35:04.119 Ctrl and D, you will get another one 670 00:35:04.119 --> 00:35:06.219 Place the new guy beside here 671 00:35:06.219 --> 00:35:10.279 Let's do it one more time 672 00:35:10.279 --> 00:35:13.229 Do the same and then this time move it to this side 673 00:35:13.229 --> 00:35:16.200 This will happen with just a click of Ctrl and D 674 00:35:16.200 --> 00:35:20.917 There shall be five spots that enemies will come out when we play it 675 00:35:20.917 --> 00:35:24.559 You can see enemies showing up like this 676 00:35:24.559 --> 00:35:26.059 Some of them moves down 677 00:35:26.059 --> 00:35:27.681 And others move toward the player 678 00:35:27.681 --> 00:35:28.959 This is what we get 679 00:35:28.959 --> 00:35:31.259 But you see that the enemies collide with each other 680 00:35:31.259 --> 00:35:33.807 Let's work with this later 681 00:35:37.359 --> 00:35:39.659 Let's move things a bit 682 00:35:39.659 --> 00:35:41.610 so that it comes with in the border 683 00:35:41.610 --> 00:35:43.610 The main Camera is indicated here like this 684 00:35:43.610 --> 00:35:47.000 Some of enemies were out of the camera because they're all spread out 685 00:35:47.000 --> 00:35:49.483 Let's gather them closer 686 00:35:55.160 --> 00:35:56.360 Like this 687 00:35:56.360 --> 00:36:00.631 They all line up inside the Main Camera area 688 00:36:00.631 --> 00:36:05.079 So we have the main EnemyManager like this 689 00:36:05.079 --> 00:36:07.829 But the CreateTime for all of them are 690 00:36:07.829 --> 00:36:10.477 the same with 2 seconds, having them all come out at the same time 691 00:36:10.477 --> 00:36:13.359 Therefore, let's play around the time here 692 00:36:13.359 --> 00:36:17.959 One would have 2.3 seconds, some others with different time 693 00:36:17.959 --> 00:36:24.506 like 2.6 seconds, let's change little by little 694 00:36:27.359 --> 00:36:30.959 If you play it, this is what happens 695 00:36:30.959 --> 00:36:34.309 Enemies don't just come out all at once 696 00:36:34.309 --> 00:36:38.480 They spawn like this 697 00:36:38.480 --> 00:36:40.480 So, we've created EnemyManager 698 00:36:40.480 --> 00:36:43.126 Here, we still have a problem, so let's take a look 699 00:36:45.487 --> 00:36:49.760 Enemies keep coming out, and I'll keep firing 700 00:36:49.760 --> 00:36:53.119 The problem here is, let me die here 701 00:36:53.119 --> 00:36:56.169 And go to Hierarchy window 702 00:36:56.169 --> 00:37:00.440 You will see Bullets and Enemies piling up like this 703 00:37:00.440 --> 00:37:03.940 I'm not even playing anymore but they are keep 704 00:37:03.940 --> 00:37:05.600 accumulating like this 705 00:37:05.600 --> 00:37:08.804 This way, the memory will keep piling up 706 00:37:08.804 --> 00:37:11.454 Eventually 707 00:37:11.454 --> 00:37:14.600 this Unity editor can turn itself off 708 00:37:14.600 --> 00:37:18.232 because it doesn't have any free memory left 709 00:37:18.232 --> 00:37:24.006 So, we need to address this problem 710 00:37:24.006 --> 00:37:27.279 To fix this, there are several ways to do it 711 00:37:27.279 --> 00:37:30.629 To solve this problem, right here 712 00:37:30.629 --> 00:37:37.239 Let's place an invisible area outside the camera like this 713 00:37:37.239 --> 00:37:39.014 We can have objects 714 00:37:39.014 --> 00:37:41.264 come to this area and collide with each other to make it disappear 715 00:37:41.264 --> 00:37:43.864 or we can count the screen to remove them 716 00:37:43.864 --> 00:37:46.359 we can solve it like so 717 00:37:46.359 --> 00:37:47.709 and another one is 718 00:37:47.709 --> 00:37:51.259 The EnemyManager we made, is automatically 719 00:37:51.259 --> 00:37:53.679 and frequently processing CreateTime 720 00:37:53.679 --> 00:37:56.679 We can have them create randomly 721 00:37:56.679 --> 00:37:58.579 Simply, have these objects 722 00:37:58.579 --> 00:38:02.079 The leftover enemies or bullets, before having these game objects 723 00:38:02.079 --> 00:38:05.640 disappear automatically as we so desire 724 00:38:05.640 --> 00:38:07.490 First, in the EnemyManager 725 00:38:07.490 --> 00:38:09.890 let's implement random spawn 726 00:38:09.890 --> 00:38:12.119 processing mechanism here 727 00:38:12.119 --> 00:38:15.769 If you want to make EnemyManger to spawn randomly 728 00:38:15.769 --> 00:38:19.880 what you want to do is, from this interval to that interval, create object 729 00:38:19.880 --> 00:38:21.580 So you decide certain time 730 00:38:21.580 --> 00:38:23.600 creating some kind of random number 731 00:38:23.600 --> 00:38:27.150 How? from 1 second to 5 seconds 732 00:38:27.150 --> 00:38:29.150 during that interval, create objects one by one 733 00:38:29.150 --> 00:38:31.399 something like this is what we want to make happen 734 00:38:31.399 --> 00:38:34.349 Therefore, let's create minimum time and 735 00:38:34.349 --> 00:38:35.919 Maximum time 736 00:38:35.919 --> 00:38:40.799 For minimum time, put float minTime 737 00:38:40.799 --> 00:38:43.000 I'll have it as 1 second 738 00:38:43.000 --> 00:38:45.039 and then for maximum time, maxTime 739 00:38:45.039 --> 00:38:47.119 I'll have it as 5 seconds 740 00:38:47.119 --> 00:38:52.200 we want to process it so that objects to be created between this interval 741 00:38:52.200 --> 00:38:55.559 Then, our initial createTime is set to 2 seconds right now 742 00:38:55.559 --> 00:38:58.409 what we want to do is that we want this value 743 00:38:58.409 --> 00:39:01.200 to become randomly picked among the times within the interval 744 00:39:01.200 --> 00:39:04.359 then we will add something called 745 00:39:04.359 --> 00:39:12.280 Random dot Range in createTime 746 00:39:12.280 --> 00:39:15.119 Remember when we were getting probability 747 00:39:15.119 --> 00:39:19.440 if we put minTime and maxTime in there like this 748 00:39:19.440 --> 00:39:21.490 it will pick a value out of the interval 749 00:39:21.490 --> 00:39:23.919 and pass it to createTime 750 00:39:23.919 --> 00:39:25.669 Then this createTime will keep 751 00:39:25.669 --> 00:39:29.799 changing within 1 seconds to 5 seconds interval 752 00:39:29.799 --> 00:39:32.799 this createTime will only create objects at the beginning, so 753 00:39:32.799 --> 00:39:37.559 How? we need it when we create the next enemy again 754 00:39:37.559 --> 00:39:41.479 so let's make another line to get 755 00:39:41.479 --> 00:39:43.779 a createTime value randomly 756 00:39:43.779 --> 00:39:46.640 again and again 757 00:39:46.640 --> 00:39:49.240 So, for this createTime 758 00:39:49.240 --> 00:39:52.200 the designer doesn't need to have it set to 2 seconds 759 00:39:52.200 --> 00:39:55.200 Since the value will be set randomly within the interval 760 00:39:55.200 --> 00:39:57.320 let's take out public from there 761 00:39:57.320 --> 00:40:00.599 now, save it and let's check it out 762 00:40:00.599 --> 00:40:03.559 EnemyManger won't be in CreateTime anymore 763 00:40:03.559 --> 00:40:06.919 Now, if you play, from second 1 to 5 764 00:40:06.919 --> 00:40:09.953 we can see that Enemies are spawning randomly 765 00:40:10.421 --> 00:40:14.311 DestryZone and collide detection 766 00:40:14.854 --> 00:40:17.559 Like we talked about earlier 767 00:40:17.559 --> 00:40:19.859 we will make a DestroyZone 768 00:40:19.859 --> 00:40:22.960 when objects get out of the play area 769 00:40:22.960 --> 00:40:24.610 in order to process the game 770 00:40:24.610 --> 00:40:28.799 to destroy objects inside 771 00:40:28.799 --> 00:40:31.960 The theory is simple as this, there is this area 772 00:40:31.960 --> 00:40:32.810 Play area 773 00:40:32.810 --> 00:40:35.599 if you select the Main Camera here, do you see the area? 774 00:40:35.599 --> 00:40:37.849 for objects move out this area 775 00:40:37.849 --> 00:40:40.119 when it touches the border 776 00:40:40.119 --> 00:40:42.000 let's have them destroyed 777 00:40:42.000 --> 00:40:46.640 Let's name this a DestroyZone 778 00:40:46.640 --> 00:40:48.840 So let's press plus button in Hierarchy 779 00:40:48.840 --> 00:40:51.479 Find Cube of 3D Object 780 00:40:51.479 --> 00:40:55.440 and name it DestryZone 781 00:40:55.440 --> 00:40:59.640 In the Transform, click reset to have them 0, 0, 0 782 00:40:59.640 --> 00:41:02.559 I will have this located up here like this 783 00:41:02.559 --> 00:41:05.159 If I place it at the bottom like this 784 00:41:05.159 --> 00:41:07.809 Enemies will collide with this DestroyZone 785 00:41:07.809 --> 00:41:09.642 right away, so 786 00:41:09.642 --> 00:41:12.224 I'd like to make the area outside of the enemy spawn area 787 00:41:15.491 --> 00:41:19.741 Shall we have the X value as 788 00:41:19.741 --> 00:41:23.851 maximum of 30? For the maximum size? 789 00:41:23.851 --> 00:41:25.260 You can reduce it if you want 790 00:41:25.260 --> 00:41:27.547 because the Main Camera area is this big 791 00:41:29.962 --> 00:41:32.400 You can have them smaller size like this 792 00:41:32.400 --> 00:41:35.200 If in the screen you are seeing 793 00:41:35.200 --> 00:41:36.840 Player looks too big 794 00:41:36.840 --> 00:41:39.559 Both enemies and the Player are too big 795 00:41:39.559 --> 00:41:41.009 the Main Camera 796 00:41:41.009 --> 00:41:43.080 we want to have bigger area for the main camera to capture 797 00:41:43.080 --> 00:41:44.799 Then how do we change this? 798 00:41:44.799 --> 00:41:48.169 This camera area, the rectangular area 799 00:41:48.169 --> 00:41:50.679 Select the Main Camera on the right 800 00:41:50.679 --> 00:41:54.479 There is a property called Size 801 00:41:54.479 --> 00:41:56.000 this is set to 5 by default 802 00:41:56.000 --> 00:41:58.250 let's change this value from 5 803 00:41:58.250 --> 00:42:01.200 to 10 just to see what happens? 804 00:42:01.200 --> 00:42:02.237 what happens? 805 00:42:04.880 --> 00:42:09.080 The rectangular area that camera is capturing got bigger 806 00:42:09.080 --> 00:42:12.479 It means, now it covers bigger area in the screen 807 00:42:12.479 --> 00:42:14.919 Since others are inside this area 808 00:42:14.919 --> 00:42:17.840 Let's have this DestroyZone 809 00:42:17.840 --> 00:42:19.840 move out of this Camera area 810 00:42:19.840 --> 00:42:22.159 It's still visible in the game screen 811 00:42:22.159 --> 00:42:24.854 So, let's move it away from the area 812 00:42:26.880 --> 00:42:29.479 So, with the Main Camera 813 00:42:29.479 --> 00:42:34.865 we can see that it is selected out of the area 814 00:42:41.280 --> 00:42:43.599 Therefore, this is the DestroyZone 815 00:42:43.599 --> 00:42:46.599 The ones below are the EnemyManagers 816 00:42:46.599 --> 00:42:50.159 We had them inside the area 817 00:42:50.159 --> 00:42:52.039 If you think the DestroyZone is way too big 818 00:42:52.039 --> 00:42:54.159 for me, it'd be enough with just value of 20 819 00:42:54.159 --> 00:42:55.200 How does it look like then? 820 00:42:55.200 --> 00:42:56.880 It comes inside the Main Camera 821 00:42:56.880 --> 00:43:01.679 But for this one as well, let's make it longer in length 822 00:43:01.679 --> 00:43:06.719 Therefore, that's it for setting Main Camera area 823 00:43:06.719 --> 00:43:09.000 We also need it somewhere else 824 00:43:09.000 --> 00:43:10.159 We need one down here as well 825 00:43:10.159 --> 00:43:13.000 But first, I'll attach a script to this 826 00:43:13.000 --> 00:43:14.880 and then proceed with the task 827 00:43:14.880 --> 00:43:18.599 First, in the Scripts folder, press plus button 828 00:43:18.599 --> 00:43:20.520 to add the C# script 829 00:43:20.520 --> 00:43:24.356 and name it the same as DestroyZone 830 00:43:27.039 --> 00:43:28.919 So now we have the DestroyZone 831 00:43:28.919 --> 00:43:30.088 In this DestroyZone 832 00:43:30.088 --> 00:43:32.688 Drag and drop this DestoryZone 833 00:43:32.688 --> 00:43:34.679 we have it created 834 00:43:34.679 --> 00:43:37.119 now let's proceed to implementing scripts 835 00:43:37.119 --> 00:43:39.783 and see if it works well 836 00:43:43.119 --> 00:43:48.359 This DestroyZone will be used in this way 837 00:43:48.359 --> 00:43:52.640 You guys are in front of a door 838 00:43:52.640 --> 00:43:54.840 There is a door like this 839 00:43:54.840 --> 00:43:57.760 When you try to open the door by turning the door knob 840 00:43:57.760 --> 00:44:01.080 Your hand and the door must touch each other 841 00:44:01.080 --> 00:44:03.340 But unlike doors like this 842 00:44:03.340 --> 00:44:06.960 let's say there is a door like this 843 00:44:06.960 --> 00:44:10.840 The two doors are attached like this 844 00:44:10.840 --> 00:44:12.359 and the question is, what's attached above them? 845 00:44:12.359 --> 00:44:14.400 There is a sensor 846 00:44:14.400 --> 00:44:19.559 so then what happens if I go into this area? 847 00:44:19.559 --> 00:44:23.799 This door automatically opens 848 00:44:23.799 --> 00:44:26.591 it followed 849 00:44:26.591 --> 00:44:30.000 these two doors opens 850 00:44:30.000 --> 00:44:32.119 then I can go through this space 851 00:44:32.119 --> 00:44:34.239 Why so? I haven't even touched anything 852 00:44:34.239 --> 00:44:38.359 Why did this door open now? 853 00:44:38.359 --> 00:44:40.719 And what that is, is that you entered 854 00:44:40.719 --> 00:44:43.119 this sensor area and were detected 855 00:44:43.119 --> 00:44:44.840 So, the second type of collide 856 00:44:44.840 --> 00:44:49.919 This is where area detection, a collision is necessary 857 00:44:49.919 --> 00:44:51.679 So, when we create a game 858 00:44:51.679 --> 00:44:55.119 we can define a virtual area within a certain range 859 00:44:55.119 --> 00:44:56.520 and if another player enters this area 860 00:44:56.520 --> 00:45:00.440 we can make it trigger an attack or similar actions 861 00:45:00.440 --> 00:45:03.880 It's like detecting an event when someone enters the area 862 00:45:03.880 --> 00:45:06.159 If you go to DestroyZone and see 863 00:45:06.159 --> 00:45:09.159 There is this Box Collider on the right side 864 00:45:09.159 --> 00:45:11.359 There, you can see Is Trigger 865 00:45:11.359 --> 00:45:13.294 Please enable this by checking the box 866 00:45:16.359 --> 00:45:18.258 This is Is Trigger 867 00:45:18.258 --> 00:45:21.320 first of all, it has a Collider, the first condition for collision 868 00:45:21.320 --> 00:45:24.440 Among them, it will be used for detecting certain events 869 00:45:24.440 --> 00:45:26.200 This is what it's about 870 00:45:26.200 --> 00:45:28.239 and then, the second condition 871 00:45:28.239 --> 00:45:30.039 what do you also need for collision? 872 00:45:30.039 --> 00:45:32.119 We need a rigid body 873 00:45:32.119 --> 00:45:34.880 We have Enemy but do we have Bullet? 874 00:45:34.880 --> 00:45:35.960 No we don't 875 00:45:35.960 --> 00:45:39.159 of course you would want to add rigid body in Bullet 876 00:45:39.159 --> 00:45:43.840 But for us, we want to process Player and Enemy at the same time 877 00:45:43.840 --> 00:45:47.588 So, we will add Rigidbody in the DestroyZone itself 878 00:45:50.320 --> 00:45:53.919 And for this one, we won't need to enable 879 00:45:53.919 --> 00:45:55.119 physical properties 880 00:45:55.119 --> 00:45:56.280 like bouncing off or pulled by gravity 881 00:45:56.280 --> 00:45:59.280 So, we are going to disable physical movement 882 00:45:59.280 --> 00:46:02.039 We we enable collision event 883 00:46:02.039 --> 00:46:03.479 what kind of event? 884 00:46:03.479 --> 00:46:06.280 Event that triggers 885 00:46:06.280 --> 00:46:10.640 Instead, physical movement of bouncing off for example, will be disabled 886 00:46:10.640 --> 00:46:13.679 For that, you will find something called Is Kinematic 887 00:46:13.679 --> 00:46:15.960 If we enable this option 888 00:46:15.960 --> 00:46:20.880 We can disable physical movement of an object 889 00:46:20.880 --> 00:46:23.840 In DestroyZone, enable Is Trigger 890 00:46:23.840 --> 00:46:26.119 and then, Is Kinematic 891 00:46:26.119 --> 00:46:27.969 In Rigidbody, enable Kinematic 892 00:46:27.969 --> 00:46:31.520 We've enabled two options 893 00:46:31.520 --> 00:46:34.920 Now back in DestroyZone 894 00:46:34.920 --> 00:46:37.520 let's write some scripts 895 00:46:37.520 --> 00:46:40.840 For the event that occurs when objects collide 896 00:46:40.840 --> 00:46:42.440 we used OnCollisionEnter 897 00:46:42.440 --> 00:46:44.840 But in here, we need a trigger event to occur 898 00:46:44.840 --> 00:46:48.280 So, we will be using OnTriggerEnter this time 899 00:46:48.280 --> 00:46:52.719 Just like the previous one, you can also see Exit and Stay 900 00:46:52.719 --> 00:46:55.555 What we want to do is 901 00:46:59.960 --> 00:47:02.359 this, destroy whichever that collided 902 00:47:02.359 --> 00:47:05.183 The zone itself won't be affected because then 903 00:47:05.183 --> 00:47:06.959 it will only be one time use thing 904 00:47:06.959 --> 00:47:12.359 So add in Destroy, write other dot gameOject 905 00:47:12.359 --> 00:47:14.972 Then this function will destroy other objects coming into contact 906 00:47:18.439 --> 00:47:22.001 Let's save and go play the game 907 00:47:24.179 --> 00:47:26.319 if I fire my bullets 908 00:47:26.319 --> 00:47:29.842 in the Scene window 909 00:47:29.842 --> 00:47:32.680 Bullets don't accumulate anymore 910 00:47:32.680 --> 00:47:35.079 You can see them disappear right when colliding with that zone 911 00:47:35.079 --> 00:47:37.079 There, we have made ourselves a DestroyZone 912 00:47:37.079 --> 00:47:39.959 Now, let's not have the zone be up here only 913 00:47:39.959 --> 00:47:44.560 let's duplicate it by pressing Ctrl and D on our keyboard down here 914 00:47:44.560 --> 00:47:46.800 Do you see the zone in the game window? 915 00:47:46.800 --> 00:47:50.239 Let's move it further down so it's not visible 916 00:47:50.239 --> 00:47:53.280 how does it look? 917 00:47:53.280 --> 00:47:57.359 DestroyZone and Enemy will be here in this hierarchy window 918 00:47:57.359 --> 00:48:00.800 If you click play button 919 00:48:00.800 --> 00:48:03.988 Bullets will be destroyed 920 00:48:03.988 --> 00:48:07.959 by the DestroyZone 921 00:48:07.959 --> 00:48:10.719 Enemies that went down will be destroyed at the bottom zone 922 00:48:10.719 --> 00:48:13.479 However, enemies that slip through 923 00:48:13.479 --> 00:48:15.160 They won't be able to collide with destroyZone 924 00:48:15.160 --> 00:48:18.199 Therefore, we need to add more zones 925 00:48:18.199 --> 00:48:20.849 So, select this and then 926 00:48:20.849 --> 00:48:22.399 We will duplicate 927 00:48:22.399 --> 00:48:24.821 another DestroyZone using Ctrl and D key 928 00:48:24.821 --> 00:48:27.520 Let's place this at 0, 0, 0 first 929 00:48:27.520 --> 00:48:29.520 and then from the 930 00:48:29.520 --> 00:48:32.096 Now elongate it along the Y axis 931 00:48:32.096 --> 00:48:36.640 have the X axis as 1 and Y axis as 20 932 00:48:36.640 --> 00:48:38.079 then it still will have some gap 933 00:48:38.079 --> 00:48:42.959 So let's increase the number by 10 934 00:48:42.959 --> 00:48:44.160 It doesn't matter if they overlap 935 00:48:44.160 --> 00:48:45.239 I will place it like this 936 00:48:45.239 --> 00:48:47.760 place it so it just gets out of the screen 937 00:48:47.760 --> 00:48:49.439 and then Ctrl and D 938 00:48:49.439 --> 00:48:53.800 For another duplicate for the opposite side 939 00:48:53.800 --> 00:48:54.880 This is how I placed them 940 00:48:54.880 --> 00:48:58.439 Now, let's play and see 941 00:48:58.439 --> 00:48:59.920 Tada, they are all gone 942 00:48:59.920 --> 00:49:02.920 Why? when collided in the DestroyZone 943 00:49:02.920 --> 00:49:05.880 we coded so that everything in there to be destroyed 944 00:49:05.880 --> 00:49:08.719 But we need to address one problem here 945 00:49:08.719 --> 00:49:10.920 So, in Unity 946 00:49:10.920 --> 00:49:14.119 we can set objects 947 00:49:14.119 --> 00:49:15.880 in groups 948 00:49:15.880 --> 00:49:18.280 and can enable or disable collision between certain groups 949 00:49:18.280 --> 00:49:20.640 We can make such physical process 950 00:49:20.640 --> 00:49:21.880 To do that 951 00:49:21.880 --> 00:49:23.160 We can pick any objects 952 00:49:23.160 --> 00:49:24.640 select gameObject 953 00:49:24.640 --> 00:49:28.359 On the right side, there is layer feature 954 00:49:28.359 --> 00:49:30.359 open the drop down list of the layer 955 00:49:30.359 --> 00:49:32.920 you will have 0 to 5 as default 956 00:49:32.920 --> 00:49:36.199 Let's click Add Layer 957 00:49:36.199 --> 00:49:38.119 Then in the Inspector window 958 00:49:38.119 --> 00:49:40.199 We get Tags and Layers 959 00:49:40.199 --> 00:49:42.880 We can register our layers here 960 00:49:42.880 --> 00:49:45.079 It will act as a group 961 00:49:45.079 --> 00:49:47.680 Here, we add a layer of DestroyZone 962 00:49:47.680 --> 00:49:48.680 Just like so 963 00:49:48.680 --> 00:49:51.280 Let's set up so that objects inside this DestroyZone layer 964 00:49:51.280 --> 00:49:53.753 do not collide with each other 965 00:49:56.000 --> 00:49:58.920 So, we've added DestroyZone layer, right? 966 00:49:58.920 --> 00:50:01.199 If you select again, they are not selected yet 967 00:50:01.199 --> 00:50:04.079 Let's select all of these 968 00:50:04.079 --> 00:50:05.719 Select DestroyZone 969 00:50:05.719 --> 00:50:08.079 And if you go to layer, we have 970 00:50:08.079 --> 00:50:09.959 that DestoryZone layer we've made in the list 971 00:50:09.959 --> 00:50:11.439 select that 972 00:50:11.439 --> 00:50:14.920 These objects are all included in the layer 973 00:50:14.920 --> 00:50:17.199 Now let's make an option 974 00:50:17.199 --> 00:50:19.680 for these objects not to collide with each other 975 00:50:19.680 --> 00:50:20.560 Where do you do that? 976 00:50:20.560 --> 00:50:22.839 Go to Edit menu 977 00:50:22.839 --> 00:50:26.640 we have the project Settings 978 00:50:26.640 --> 00:50:28.400 and if you open it 979 00:50:28.400 --> 00:50:32.920 there is a category called Physics 980 00:50:32.920 --> 00:50:35.959 there, you will see many options on the right side 981 00:50:35.959 --> 00:50:40.040 We then see the collision matrix 982 00:50:40.040 --> 00:50:42.119 Right? In there 983 00:50:42.119 --> 00:50:44.719 The DestoryZone we've created is here 984 00:50:44.719 --> 00:50:45.680 and then where again? 985 00:50:45.680 --> 00:50:47.640 Also here in horizontal axis 986 00:50:47.640 --> 00:50:50.280 So, at which point do they meet? 987 00:50:50.280 --> 00:50:54.280 This overlapping part, exactly this part here 988 00:50:54.280 --> 00:50:56.560 between these objects, what does it mean? 989 00:50:56.560 --> 00:50:58.400 If the box is checked, they can collide 990 00:50:58.400 --> 00:51:00.199 So if you disable it 991 00:51:00.199 --> 00:51:02.680 we do not let them collide with each other 992 00:51:02.680 --> 00:51:05.959 That's what it means 993 00:51:05.959 --> 00:51:08.479 I will disable it 994 00:51:08.479 --> 00:51:10.359 then, the objects inside this layer 995 00:51:10.359 --> 00:51:11.760 What happens? They do not collide 996 00:51:11.760 --> 00:51:13.319 Let's click the play button 997 00:51:13.319 --> 00:51:17.760 Then, the zones don't collide with each other 998 00:51:17.760 --> 00:51:20.439 Player spawns 999 00:51:20.439 --> 00:51:22.079 Even if it fires 1000 00:51:22.079 --> 00:51:29.040 Bullets gets destroyed by the zone 1001 00:51:29.040 --> 00:51:34.000 Enemies, no matter where Player is 1002 00:51:34.000 --> 00:51:36.079 They move towards the walls and collide 1003 00:51:36.079 --> 00:51:37.719 colliding with the zone at the bottom 1004 00:51:37.719 --> 00:51:40.239 They are all in here 1005 00:51:40.239 --> 00:51:43.199 And if you take a look at Hierarchy window 1006 00:51:43.199 --> 00:51:45.359 even if they appear, when they gets out of the screen 1007 00:51:45.359 --> 00:51:49.280 you can see them destroyed like this 1008 00:51:49.280 --> 00:51:52.480 This way, we can stop memories 1009 00:51:52.480 --> 00:51:53.880 being wasted piling up 1010 00:51:53.880 --> 00:51:56.079 Here, additionally 1011 00:51:56.079 --> 00:51:58.239 we need to see what's going on the Enemies 1012 00:51:58.239 --> 00:52:01.520 Did you see Enemies colliding each other destroying each other? 1013 00:52:01.520 --> 00:52:05.839 They collide with each other and destroyed each other 1014 00:52:05.839 --> 00:52:09.160 So, things like enemies colliding and dying 1015 00:52:09.160 --> 00:52:11.800 or the player and bullets colliding and dying 1016 00:52:11.800 --> 00:52:14.520 needs to be prevented 1017 00:52:14.520 --> 00:52:16.959 So, select any objects 1018 00:52:16.959 --> 00:52:19.920 add it to a layer 1019 00:52:19.920 --> 00:52:22.170 In the layer section 1020 00:52:22.170 --> 00:52:23.520 Below DestroyZone layer 1021 00:52:23.520 --> 00:52:25.959 register Player 1022 00:52:25.959 --> 00:52:29.400 and then Bullet as well? 1023 00:52:29.400 --> 00:52:32.160 Also of course, add an Enemy layer in there 1024 00:52:32.160 --> 00:52:36.599 Now let's prevent collision between objects within each layers 1025 00:52:36.599 --> 00:52:41.319 Just like before, in the Edit menu, go to Project settings 1026 00:52:41.319 --> 00:52:43.319 and in the Physics 1027 00:52:43.319 --> 00:52:45.599 We also can see the layer we've just added 1028 00:52:45.599 --> 00:52:49.040 Enemy and Enemy, should they collide with each other? No 1029 00:52:49.040 --> 00:52:51.199 Bullet and Bullet or Player 1030 00:52:51.199 --> 00:52:54.319 Do they collide with each other? 1031 00:52:54.319 --> 00:52:56.199 There is only one Player 1032 00:52:56.199 --> 00:52:58.218 So we don't need to worry about it here 1033 00:53:01.119 --> 00:53:05.199 So let's finish setting up and then play again 1034 00:53:05.199 --> 00:53:08.439 Enemies never collide with each other 1035 00:53:08.439 --> 00:53:10.719 But they collide with the Player and Bullets 1036 00:53:10.719 --> 00:53:14.239 Why? the Enemy or the Player 1037 00:53:14.239 --> 00:53:17.640 Haven't had been set up with the layers 1038 00:53:17.640 --> 00:53:19.920 Now select the Player first 1039 00:53:19.920 --> 00:53:25.239 In the layer list, check 7th Player layer 1040 00:53:25.239 --> 00:53:29.680 If you choose yes, all the child objects 1041 00:53:29.680 --> 00:53:33.560 will also be assigned to the Player layer 1042 00:53:33.599 --> 00:53:37.319 and then, if you go to Prefabs folder, there is Bullet 1043 00:53:37.319 --> 00:53:38.479 Select Bullet first 1044 00:53:38.479 --> 00:53:41.920 In the Inspector window on the right, change the layer into which? 1045 00:53:41.920 --> 00:53:44.400 into Bullet 1046 00:53:44.400 --> 00:53:48.100 And For Enemy, which layer? To Enemy 1047 00:53:48.100 --> 00:53:49.479 If you switch things up like this 1048 00:53:49.479 --> 00:53:53.439 officially we are all done 1049 00:53:53.439 --> 00:53:54.880 To summarize 1050 00:53:54.880 --> 00:53:57.400 To make groups that we want 1051 00:53:57.400 --> 00:53:58.880 We add layers 1052 00:53:58.880 --> 00:54:04.800 And then we set each object’s layer values like this 1053 00:54:04.800 --> 00:54:08.160 Next, for collision checks among objects on each layer 1054 00:54:08.160 --> 00:54:12.599 We go to Edit to Project Settings then, Physics 1055 00:54:12.599 --> 00:54:15.880 which is called Physics Matrix, and configure the collision settings 1056 00:54:15.880 --> 00:54:18.160 for the overlapping sections 1057 00:54:18.160 --> 00:54:22.000 That concludes the concept of collisions so far 1058 00:54:22.000 --> 00:54:25.719 Let me summarize what we’ve covered in this chapter 1059 00:54:25.719 --> 00:54:27.119 As the first topic 1060 00:54:27.119 --> 00:54:30.040 we learned about handling object collisions 1061 00:54:30.040 --> 00:54:31.590 We explored colliders 1062 00:54:31.590 --> 00:54:34.520 and rigidBody as the prerequisites for collisions 1063 00:54:34.520 --> 00:54:36.560 and learned how to use event handling functions 1064 00:54:36.560 --> 00:54:39.439 to detect those collisions 1065 00:54:39.439 --> 00:54:43.119 Secondly, we covered how to automatically spawn enemies 1066 00:54:43.119 --> 00:54:45.119 We created an enemy prefab 1067 00:54:45.119 --> 00:54:47.250 and added logic through the EnemyManager object 1068 00:54:47.250 --> 00:54:49.100 and script to spawn enemies 1069 00:54:49.100 --> 00:54:51.920 at set intervals and at random times 1070 00:54:51.920 --> 00:54:56.359 Lastly, we learned about the DestroyZone and collision detection 1071 00:54:56.359 --> 00:54:57.959 From the accumulation of objects 1072 00:54:57.959 --> 00:55:00.959 we learned that it can lead to memory issues 1073 00:55:00.959 --> 00:55:03.839 To address this issue, we created a DestroyZone object 1074 00:55:03.839 --> 00:55:05.389 and utilized trigger events 1075 00:55:05.389 --> 00:55:07.540 to remove any off screen objects 1076 00:55:09.520 --> 00:55:11.079 That concludes everything so far and great work 1077 00:55:11.756 --> 00:55:12.856 Object Collision Handling Requirements for collision Collider, serving as the physical body of the object Rigidbody, handling the physics aspects 1078 00:55:12.856 --> 00:55:14.006 Colliding Between Objects The default collider attached to the Enemy GameObject is a BoxCollider 1079 00:55:14.006 --> 00:55:15.156 Add a Rigidbody component to the Enemy GameObject by selecting Rigidbody under the Physics category Uncheck the Use Gravity property to disable gravity 1080 00:55:15.156 --> 00:55:16.270 Destroying Colliding Objects The function that detects collisions is OnCollisionEnter() By using OnCollisionEnter(), you can destroy both the object 1081 00:55:16.270 --> 00:55:17.020 Automatic Enemy Spawning Creating and Removing Enemy Prefabs Drag, drop Enemy GameObject from Hierarchy window into Prefabs in the project for prefab 1082 00:55:17.020 --> 00:55:17.820 Delete the Enemy GameObject in the Hierarchy by selecting it and pressing the Delete key 1083 00:55:17.820 --> 00:55:18.620 Creating an EnemyManager GameObject Configure the EnemyManager to spawn enemies at regular intervals, and have them at the EnemyManager’s location 1084 00:55:18.620 --> 00:55:19.320 In the Hierarchy window, select plus, Create Empty to rename it, EnemyManager Set the icon gizmo so that it is visible after placing it in an appropriate location in the scene 1085 00:55:19.320 --> 00:55:20.004 Create an EnemyManager script in the Scripts folder of the Project window, then assign it to the EnemyManager GameObject in the Hierarchy 1086 00:55:20.004 --> 00:55:21.304 Implementing the Enemy-Spawning Script Declare the necessary properties in EnemyManager.cs, then assign the Enemy prefab to the Enemy Factory property 1087 00:55:21.304 --> 00:55:22.654 Enemy Spawning Flow, time passes, spawn time arrives, spawns enemy By resetting the elapsed time, we solved the issue of continuously spawning enemies 1088 00:55:22.654 --> 00:55:24.041 Duplicate the EnemyManager GameObject and place it appropriately In EnemyManager.cs, add properties for minTime and maxTime, and apply a random time 1089 00:55:24.041 --> 00:55:24.791 DestroyZone and Collision Detection Create a Destroy Zone object and script Remove objects that go off-screen to prevent memory waste 1090 00:55:24.791 --> 00:55:25.441 To create an object that will detect the area, click the plus button in the hierarchy tool bar, 1091 00:55:25.441 --> 00:55:26.041 then select 3D Object, Cube. Rename the cube to DestroyZoneU 1092 00:55:26.041 --> 00:55:26.682 Set the Trigger option for event detection Add the necessary Rigidbody component to the object for collision detection 1093 00:55:26.682 --> 00:55:27.352 Create a C# script in the Assets Scripts folder in the Project window, then name it DestroyZone 1094 00:55:27.352 --> 00:55:28.072 Add the DestroyZone script as a component to the DestroyZoneU object in the Hierarchy In the OnTriggerEnter function, write the code to remove objects 1095 00:55:28.072 --> 00:55:28.779 Prevent collision between DestroyZone objects When DestroyZone objects overlap, they collide with each other and get destroyed 1096 00:55:28.779 --> 00:55:29.433 To prevent collision between layers, select the Layer drop down menu in the Inspector window, open the Add Layer window, and add a DestroyZone layer 1097 00:55:29.433 --> 00:55:29.918 Select all DestroyZone objects in the Hierarchy, then change the layer to DestroyZone from drop down list in the Inspector window 1098 00:55:29.918 --> 00:55:30.444 Select Edit, Project settings, open the Project settings window 1099 00:55:30.444 --> 00:55:31.079 Select the Physics section, scroll down to the Matrix at the bottom, and uncheck the box where DestroyZone layers overlap to disable collisions 1100 00:55:31.079 --> 00:55:31.577 Add three layers under the Layers section: Player, Bullet, and Enemy 1101 00:55:31.577 --> 00:55:32.129 Select Edit, Project Settings to open the window of Physics, check Layer Collision Matrix