1 00:00:03,920 --> 00:00:04,910 ‫Welcome back. 2 00:00:04,910 --> 00:00:08,740 ‫I hope you're up to speed on some fruit's healthy fruits. 3 00:00:08,750 --> 00:00:09,350 ‫Let's go. 4 00:00:09,380 --> 00:00:10,190 ‫So what? 5 00:00:10,190 --> 00:00:15,980 ‫Our goal for this video is to spawn some fruits which will be spawned from the bottom and will shoot 6 00:00:15,980 --> 00:00:21,020 ‫up into our game scene or into our game view and then we can slice them. 7 00:00:21,290 --> 00:00:26,480 ‫So far we can't slice them, but we will be able to do so later on and we want to do that randomly. 8 00:00:26,480 --> 00:00:30,890 ‫So we want to create a random fruit and with a random distance in time. 9 00:00:30,890 --> 00:00:34,820 ‫So it shouldn't be created after exactly the same amount of time. 10 00:00:34,820 --> 00:00:41,270 ‫So let's create a new script for that and let's call that spanner, because that's what it's going to 11 00:00:41,270 --> 00:00:41,450 ‫be. 12 00:00:41,450 --> 00:00:42,530 ‫It's going to be a spanner. 13 00:00:42,560 --> 00:00:44,210 ‫So let's open that up. 14 00:00:44,240 --> 00:00:49,760 ‫And here we won't need the update method, but we will need several variables. 15 00:00:49,760 --> 00:00:52,430 ‫So for one, we'll need a game object. 16 00:00:52,430 --> 00:00:58,040 ‫So public game object and that one will be the fruit to spawn. 17 00:00:58,040 --> 00:01:02,900 ‫So the fruit that we want to spawn, it could be an orange, it could be a banana and so forth. 18 00:01:02,900 --> 00:01:05,150 ‫And we want to know which fruit we should spawn. 19 00:01:05,450 --> 00:01:13,250 ‫Then we want to know how much time we want to have between the different fruits. 20 00:01:13,250 --> 00:01:18,020 ‫So it should be, let's say, between 3.3 and 1 seconds. 21 00:01:18,020 --> 00:01:29,180 ‫So let's create a float min weight and that would be 0.3 seconds and the public float max weight. 22 00:01:29,420 --> 00:01:34,400 ‫So the maximum amount of time that we need to wait for a fruit should be 1/2. 23 00:01:34,400 --> 00:01:38,750 ‫And if we do it that way, we can easily add that within unity. 24 00:01:38,750 --> 00:01:40,700 ‫So that will allow us to do that. 25 00:01:41,120 --> 00:01:47,480 ‫Now within the START method, I want to start a routine which will constantly create fruits after well 26 00:01:47,480 --> 00:01:49,490 ‫this particular time that we have here. 27 00:01:49,640 --> 00:01:56,660 ‫And in order to do that, we need to use the start code routine method, start code routine. 28 00:01:56,660 --> 00:02:01,520 ‫And as you can see here, the START code routine needs an AI enumerator. 29 00:02:01,850 --> 00:02:09,620 ‫In order to start anything, we could as well start a method or start a method with an object. 30 00:02:09,620 --> 00:02:12,950 ‫But what we really want to have is this one here. 31 00:02:13,130 --> 00:02:16,010 ‫The first overload which needs an AI enumerator. 32 00:02:16,130 --> 00:02:19,940 ‫So start code routine of course, now needs an enumerator. 33 00:02:19,940 --> 00:02:24,620 ‫So let's comment that out now and let's create that AI enumerator. 34 00:02:24,620 --> 00:02:31,670 ‫And in order to do so, we need to create a method which will return an AI enumerator and it's supports 35 00:02:31,670 --> 00:02:34,760 ‫a simple iteration over non generic collection. 36 00:02:34,760 --> 00:02:41,050 ‫So that will allow us to do is to do things well to call this method multiple times and a start code 37 00:02:41,060 --> 00:02:45,020 ‫routine and let's go ahead and spawn fruits. 38 00:02:45,020 --> 00:02:52,910 ‫That's how I'm going to call it and what I want to check here while true. 39 00:02:52,910 --> 00:02:58,280 ‫So as long as this code routine runs, I want to spawn fruits. 40 00:02:58,790 --> 00:03:05,060 ‫So what I want to do in here in the wild loop is to yield return. 41 00:03:05,060 --> 00:03:08,420 ‫So it's not a basic return statement that we have used yet. 42 00:03:08,480 --> 00:03:14,510 ‫It's a yield return which you can use for new wait 4 seconds. 43 00:03:15,200 --> 00:03:22,820 ‫So if you want to do something after an amount of time, you can use this enumerator with yield return. 44 00:03:22,820 --> 00:03:30,980 ‫So wait for a seconds and I want it to be a random value between our range of minimum weight and max 45 00:03:30,980 --> 00:03:31,520 ‫weight. 46 00:03:36,960 --> 00:03:46,680 ‫And now what I will do in here is simply debug the log and fruits, get more fruit, get spawned. 47 00:03:51,850 --> 00:03:52,570 ‫All right. 48 00:03:52,960 --> 00:03:59,520 ‫Let's say that script and actually we need to call spawn fruit in our code routine. 49 00:03:59,530 --> 00:04:00,040 ‫Of course. 50 00:04:00,040 --> 00:04:02,440 ‫So spawn fruits. 51 00:04:03,760 --> 00:04:05,380 ‫Now that all works out. 52 00:04:06,250 --> 00:04:12,160 ‫And let's save the script once again and let's go back to Unity. 53 00:04:13,000 --> 00:04:16,810 ‫All right, in here, let's create a new empty object. 54 00:04:16,810 --> 00:04:18,850 ‫And I'm just going to call that spanner. 55 00:04:23,870 --> 00:04:27,740 ‫And let's get rid of all the oranges that we had here. 56 00:04:29,360 --> 00:04:34,760 ‫The lead and that's Boehner object, of course, needs a component. 57 00:04:34,760 --> 00:04:38,200 ‫So let's search for a script called Boehner here. 58 00:04:38,210 --> 00:04:41,480 ‫And as you can see, we could change the min weight in max weight any time. 59 00:04:41,480 --> 00:04:46,070 ‫Now, the fruit to spawn is, of course, the spawn fruit or the fruit that we want to spawn. 60 00:04:46,070 --> 00:04:50,020 ‫And in our case, it's our orange. 61 00:04:50,030 --> 00:04:55,150 ‫So let's go to the sponsor and drag our orange in here fruit to spawn. 62 00:04:55,160 --> 00:04:56,750 ‫And now let's just start that. 63 00:04:56,750 --> 00:05:01,310 ‫So the only thing that will happen now is, of course, that it will go to our console and you can see 64 00:05:01,310 --> 00:05:04,220 ‫fruit get spawned five, six, seven, eight. 65 00:05:04,220 --> 00:05:06,440 ‫So constantly fruit are spawned. 66 00:05:06,440 --> 00:05:07,310 ‫So that's good. 67 00:05:07,310 --> 00:05:11,540 ‫But of course, we don't see much yet and we'll need to change that. 68 00:05:12,110 --> 00:05:12,500 ‫All right. 69 00:05:12,500 --> 00:05:18,200 ‫So we could spawn from the spawn here, which has a specific position, as you can see. 70 00:05:18,200 --> 00:05:19,010 ‫Well, it's here. 71 00:05:19,010 --> 00:05:20,390 ‫You can't see it very well. 72 00:05:20,390 --> 00:05:22,760 ‫So let's add a gizmo. 73 00:05:22,760 --> 00:05:30,200 ‫So here on the right hand side, just next to the name of the spanner, you can choose to add an a little 74 00:05:30,200 --> 00:05:30,770 ‫look for it. 75 00:05:30,770 --> 00:05:35,840 ‫So as you can see here, now I have this let's use this purple color here. 76 00:05:35,840 --> 00:05:37,430 ‫So this is going to be the spanner. 77 00:05:37,430 --> 00:05:43,760 ‫This is just a gizmo, which allows me to see where an object is, even though it's not visible in the 78 00:05:43,760 --> 00:05:44,000 ‫game. 79 00:05:44,000 --> 00:05:48,080 ‫So it doesn't have any Sprite or any animation or things like that. 80 00:05:48,080 --> 00:05:49,760 ‫No match, nothing like that. 81 00:05:49,760 --> 00:05:55,130 ‫And only has this little gizmo now which will show me where it is in my game view. 82 00:05:55,250 --> 00:05:58,670 ‫So this is currently where it is and. 83 00:06:00,580 --> 00:06:03,640 ‫Let's drag it down a little bit. 84 00:06:04,420 --> 00:06:07,090 ‫Actually, let's position it or let's reset it first. 85 00:06:07,090 --> 00:06:12,670 ‫So it's going to be at 000 and I'm just going to call that spawn or. 86 00:06:14,750 --> 00:06:16,430 ‫Or spawned plays. 87 00:06:17,270 --> 00:06:19,370 ‫So this is where it's going to spawn. 88 00:06:19,700 --> 00:06:24,500 ‫Now let's duplicate that spawn place and drag it to the side a little bit. 89 00:06:25,460 --> 00:06:32,070 ‫And let's duplicate that one again, once again, and drag that to the side as well. 90 00:06:32,090 --> 00:06:35,960 ‫So now we have those three pointers about the position. 91 00:06:35,990 --> 00:06:37,630 ‫Don't worry about that yet. 92 00:06:37,640 --> 00:06:39,580 ‫We will check it out later on. 93 00:06:39,590 --> 00:06:44,060 ‫We will play around with the right position and find what the perfect position for it is. 94 00:06:44,870 --> 00:06:48,430 ‫Now about the two spawn places which are on the side. 95 00:06:48,440 --> 00:06:49,730 ‫So spawn place too. 96 00:06:49,730 --> 00:06:51,200 ‫And in my case it's the first one. 97 00:06:51,200 --> 00:06:56,330 ‫In your case it could be different ones, but the ones here to decide should not simply shoot to the 98 00:06:56,330 --> 00:06:56,750 ‫top. 99 00:06:56,750 --> 00:06:59,900 ‫They should slightly shoot towards the center. 100 00:06:59,900 --> 00:07:01,310 ‫So we need to rotate them. 101 00:07:01,310 --> 00:07:07,280 ‫We could either rotate them this way, so I'm going to rotate it like that, or I could simply add the 102 00:07:07,280 --> 00:07:08,300 ‫value in here. 103 00:07:08,300 --> 00:07:12,980 ‫So I'm going to say rotate by 30 degrees and I'll spawn plus two. 104 00:07:12,980 --> 00:07:16,350 ‫In my case should have -30 for the Z value. 105 00:07:16,370 --> 00:07:19,040 ‫So now both will rotate a little bit. 106 00:07:19,880 --> 00:07:20,240 ‫All right. 107 00:07:20,240 --> 00:07:21,230 ‫So next. 108 00:07:21,740 --> 00:07:23,720 ‫So now let's go back to our script. 109 00:07:24,980 --> 00:07:30,410 ‫Now that we have this -- and in our spawn fruit, we will need to have the different spawn places. 110 00:07:30,500 --> 00:07:32,600 ‫The spawn should know about the spawn places. 111 00:07:32,600 --> 00:07:39,710 ‫So let's create a transform array, which is our spawn places. 112 00:07:43,050 --> 00:07:52,110 ‫And now within our spawn fruits, we don't only want to debug, but we also want to transform the position 113 00:07:52,110 --> 00:07:53,550 ‫of the fruit that is created. 114 00:07:53,550 --> 00:08:08,850 ‫So transform t is born places at the position of random range and it should be a random position between 115 00:08:08,850 --> 00:08:12,810 ‫zero and spawn places dot length. 116 00:08:12,810 --> 00:08:20,370 ‫So what that means is however many spawn places we create, we want to just assign the position of our 117 00:08:20,550 --> 00:08:25,800 ‫fruit that we created at a random position of those places. 118 00:08:26,310 --> 00:08:31,230 ‫So it means it will be either at the first spawn place, the second, the third and so forth. 119 00:08:31,230 --> 00:08:35,490 ‫So however many we assign, that's what we say here spawn places dot length. 120 00:08:35,490 --> 00:08:37,950 ‫That's the spawn place that we want to choose here. 121 00:08:37,950 --> 00:08:47,460 ‫So the index of it will be that now let's create the game object, which is our fruit, and let's instantiate 122 00:08:47,460 --> 00:08:48,060 ‫that 123 00:08:51,060 --> 00:08:53,670 ‫and we want to instantiate the fruit to spawn. 124 00:08:53,670 --> 00:08:59,820 ‫So the fruit that we want to spawn at the position of our T so off the transform variable that we have 125 00:08:59,820 --> 00:09:03,180 ‫just created here, t dot position 126 00:09:05,610 --> 00:09:07,560 ‫and DT dot rotation. 127 00:09:11,550 --> 00:09:14,460 ‫All right, let's go back to Unity and check that out. 128 00:09:14,460 --> 00:09:20,640 ‫So now it should those partners actually, we didn't assign those points yet and we don't have a sponsor 129 00:09:20,640 --> 00:09:23,460 ‫anymore, so let's change that. 130 00:09:23,500 --> 00:09:23,820 ‫Right. 131 00:09:23,820 --> 00:09:29,670 ‫So the one that I've deleted, it's created create empty spawn. 132 00:09:30,480 --> 00:09:33,930 ‫And now let's add the component script here. 133 00:09:37,730 --> 00:09:38,600 ‫And was a sponsor. 134 00:09:38,630 --> 00:09:40,550 ‫Now we need fruits to spawn. 135 00:09:40,550 --> 00:09:44,270 ‫And we're going to start off with the orange again, as I said. 136 00:09:45,170 --> 00:09:47,960 ‫And the next thing is we need the spawn places. 137 00:09:47,960 --> 00:09:50,420 ‫So we need to assign how many spawn places we have. 138 00:09:50,420 --> 00:09:53,090 ‫And for now we have three different spawn places. 139 00:09:53,090 --> 00:09:58,550 ‫And I'm just going to drag the different spawn places into my script here. 140 00:09:59,360 --> 00:09:59,750 ‫All right. 141 00:09:59,750 --> 00:10:02,480 ‫So now let's start the game and see what happens. 142 00:10:03,460 --> 00:10:09,160 ‫And as you can see now, we randomly get fruits created in our game. 143 00:10:09,400 --> 00:10:11,920 ‫Now what you don't see here in this view. 144 00:10:11,920 --> 00:10:14,320 ‫So let's deactivate, maximize on play. 145 00:10:14,770 --> 00:10:17,320 ‫Let's see what happens if we start it here. 146 00:10:17,350 --> 00:10:23,830 ‫As you can see, we create oranges and we're going to create many, many oranges because we never delete 147 00:10:23,830 --> 00:10:24,330 ‫them. 148 00:10:24,340 --> 00:10:26,820 ‫So far, we never get rid of those. 149 00:10:26,830 --> 00:10:28,730 ‫That's something that we'll have to take care of. 150 00:10:28,750 --> 00:10:29,440 ‫Of course. 151 00:10:29,440 --> 00:10:33,820 ‫And of course, also we need to change the direction to which they shoot. 152 00:10:33,820 --> 00:10:39,870 ‫So currently they just fall down and they don't have any force applied to them to to fly to the air. 153 00:10:39,880 --> 00:10:41,410 ‫So that's what we want to change. 154 00:10:41,410 --> 00:10:43,090 ‫So let's go ahead and do that. 155 00:10:43,420 --> 00:10:46,330 ‫Let's apply a little force to our fruits. 156 00:10:49,770 --> 00:10:51,510 ‫And how we can do that. 157 00:10:51,540 --> 00:11:02,310 ‫How can we do that while we get the component rigid body to DX of our fruit and then we apply a force. 158 00:11:02,310 --> 00:11:06,800 ‫So add force and we have two different overloads. 159 00:11:06,810 --> 00:11:10,260 ‫We are going to use the one here, the force and the mode. 160 00:11:10,260 --> 00:11:19,050 ‫So the force that we want to apply is transform up multiplied with ten. 161 00:11:21,390 --> 00:11:23,490 ‫So that's just a value that I've assigned here. 162 00:11:23,490 --> 00:11:27,510 ‫And then the force mode to. 163 00:11:27,510 --> 00:11:33,600 ‫DX So the way we want to force them up is going to be impulse. 164 00:11:35,760 --> 00:11:40,170 ‫So for small to be actually impulse like that. 165 00:11:42,630 --> 00:11:42,900 ‫All right. 166 00:11:42,900 --> 00:11:46,230 ‫Let's save the script and let's go back to Unity. 167 00:11:46,260 --> 00:11:47,610 ‫Let's see what happens. 168 00:11:51,830 --> 00:11:58,310 ‫And as you can see, they are shot up and then they hit each other and then fall down again. 169 00:11:58,610 --> 00:12:01,760 ‫Now, what they don't have is the rotation, so I don't like that. 170 00:12:01,790 --> 00:12:04,850 ‫So let's have a look at the different places. 171 00:12:06,800 --> 00:12:10,820 ‫Now of course our spawn places will need the spawn or script anymore. 172 00:12:10,820 --> 00:12:16,730 ‫So let's get rid of that remove component from our spawn places. 173 00:12:17,180 --> 00:12:17,630 ‫All right. 174 00:12:17,630 --> 00:12:19,940 ‫So let's remove the spawn here as well. 175 00:12:20,270 --> 00:12:27,320 ‫All right, now let's quickly fix the problem that we have unlimited of fruits, so at least that we 176 00:12:27,320 --> 00:12:29,570 ‫don't destroy the fruits after a while. 177 00:12:29,750 --> 00:12:31,190 ‫So let's do that. 178 00:12:32,630 --> 00:12:38,180 ‫So here we just say destroy fruit after 5 seconds. 179 00:12:38,180 --> 00:12:43,070 ‫So the game object that we instantiate here, we want to destroy it after 5 seconds. 180 00:12:43,070 --> 00:12:46,340 ‫So as soon as it was in the air and then it fell down. 181 00:12:46,340 --> 00:12:48,060 ‫So it's fine to destroy it. 182 00:12:48,080 --> 00:12:49,520 ‫So let's have a look here. 183 00:12:50,620 --> 00:12:55,990 ‫What happens is will spawn and then they will get destroyed after a couple of seconds. 184 00:12:56,470 --> 00:12:58,510 ‫So we have five, six, seven, eight. 185 00:12:58,510 --> 00:13:01,860 ‫And as you can see, the oranges get destroyed again. 186 00:13:01,870 --> 00:13:02,620 ‫That's good. 187 00:13:04,290 --> 00:13:06,030 ‫It still doesn't work that we rotate them. 188 00:13:06,030 --> 00:13:11,530 ‫So the force goes in towards the direction that we rotated the spawn places to. 189 00:13:11,550 --> 00:13:12,960 ‫So let's have a look at that. 190 00:13:14,760 --> 00:13:17,700 ‫And the missing rotation has to do with one thing. 191 00:13:17,700 --> 00:13:25,470 ‫So let's go back to our code as what we do here is we transform up something by ten and in this case 192 00:13:25,470 --> 00:13:31,390 ‫we transform our script up so that we add a force to the spinner itself. 193 00:13:31,410 --> 00:13:36,360 ‫What we do want, however, is to transform up our fruit. 194 00:13:36,390 --> 00:13:43,110 ‫We want to add the force from our T, so which comes from our transform position, from our spawn place. 195 00:13:43,110 --> 00:13:46,980 ‫So we don't want that to from our spawn or itself, but from our spawn place. 196 00:13:46,980 --> 00:13:49,770 ‫And our spawn place has a specific rotation. 197 00:13:49,770 --> 00:13:55,440 ‫So now let's start that again and see how our fruits are flying to the different directions. 198 00:13:57,930 --> 00:14:01,470 ‫And as you can see, they are flying in different directions. 199 00:14:01,470 --> 00:14:03,570 ‫And let's have a look. 200 00:14:03,570 --> 00:14:12,510 ‫I have played around with the values here, so this one should be 30, that one should be -30. 201 00:14:13,320 --> 00:14:17,490 ‫And we can use a spawn place in the center with zero. 202 00:14:17,490 --> 00:14:19,680 ‫So now let's go ahead and do that again. 203 00:14:19,680 --> 00:14:25,920 ‫And you will see that they will fly towards different directions and currently they can even touch each 204 00:14:25,920 --> 00:14:26,370 ‫other. 205 00:14:26,370 --> 00:14:29,100 ‫But we're going to take care of that later on as well. 206 00:14:31,980 --> 00:14:39,060 ‫Now, what we need to change is our spawn places position because currently it's they're all a little 207 00:14:39,060 --> 00:14:40,920 ‫bit too high. 208 00:14:40,920 --> 00:14:51,060 ‫So let's move all of them a little bit down to around minus five or actually we can enter minus five 209 00:14:51,090 --> 00:14:53,910 ‫here and now. 210 00:14:53,910 --> 00:14:57,390 ‫Next, I want to change the power with which they are shot. 211 00:14:57,390 --> 00:15:00,240 ‫So let's have a look how they are spawning currently. 212 00:15:00,240 --> 00:15:03,840 ‫So as you can see, they barely make it to the center. 213 00:15:03,960 --> 00:15:05,430 ‫So let's change that. 214 00:15:05,460 --> 00:15:08,760 ‫In order to do that, we need to change the transform up. 215 00:15:08,760 --> 00:15:10,020 ‫So now it's ten. 216 00:15:10,020 --> 00:15:13,860 ‫But what we could have is a random force with which they are shot. 217 00:15:13,860 --> 00:15:24,870 ‫And let's create two additional variables for that public float main force is, let's say 12 and public 218 00:15:24,870 --> 00:15:30,360 ‫float max force is, for example, 17. 219 00:15:30,360 --> 00:15:33,090 ‫So these are just values that I played around with. 220 00:15:33,090 --> 00:15:42,630 ‫And now let's multiply with random range min force and max force. 221 00:15:42,630 --> 00:15:49,410 ‫So now what it will do is it will give us a random value and we'll add the force randomly now between 222 00:15:49,410 --> 00:15:54,750 ‫12 and 17, and we can easily adjust that within our settings here. 223 00:15:54,750 --> 00:15:58,440 ‫So within our spawn we will be able to change that. 224 00:15:58,440 --> 00:16:00,930 ‫So you can see min force and max force. 225 00:16:00,930 --> 00:16:02,430 ‫So let's start that. 226 00:16:02,430 --> 00:16:06,120 ‫We can then check out directly whether that's good values or not. 227 00:16:06,120 --> 00:16:14,700 ‫As you can see, they fly up and maybe we could even go further here min force, let's say 15 and max 228 00:16:14,700 --> 00:16:21,270 ‫force 25 and then you will see that they will even fly off the screen completely and fall down again. 229 00:16:21,360 --> 00:16:25,530 ‫So maybe 12 and 17 are good values after all. 230 00:16:25,860 --> 00:16:26,550 ‫All right. 231 00:16:26,880 --> 00:16:29,640 ‫So that's it about the Spooners. 232 00:16:29,670 --> 00:16:34,890 ‫Actually, we can still slice our fruits, so let's spawn some and slice them. 233 00:16:34,890 --> 00:16:39,870 ‫As you can see, if we have multiple in the scene, we can still slice them. 234 00:16:39,870 --> 00:16:44,550 ‫But of course, we want to do that with our mouse and not with our keyboard. 235 00:16:44,850 --> 00:16:45,240 ‫Great. 236 00:16:45,240 --> 00:16:49,770 ‫So in the next video, we are going to have a look at the blade. 237 00:16:49,770 --> 00:16:51,270 ‫So we're going to create the blade. 238 00:16:51,270 --> 00:16:51,780 ‫Look. 239 00:16:51,810 --> 00:16:52,560 ‫See you there.