1 00:00:04,210 --> 00:00:05,050 ‫Welcome back. 2 00:00:05,080 --> 00:00:10,170 ‫In this chapter, we are going to build a fruit ninja clone and you might know the game fruit ninja 3 00:00:10,180 --> 00:00:10,920 ‫if you don't know it. 4 00:00:10,930 --> 00:00:12,790 ‫And check out some YouTube videos. 5 00:00:12,790 --> 00:00:21,280 ‫It's a very basic game in which you have to slice some fruit and you simply do that with a swipe and 6 00:00:21,280 --> 00:00:27,100 ‫then you slice them, they fall off and you get some points and your goal is to get as many points as 7 00:00:27,100 --> 00:00:27,740 ‫possible. 8 00:00:27,760 --> 00:00:32,260 ‫Of course, there is something that holds you back from making points and that are bombs. 9 00:00:32,260 --> 00:00:34,600 ‫So as soon as you cut a bomb, you'll lose the game. 10 00:00:35,110 --> 00:00:35,440 ‫All right. 11 00:00:35,440 --> 00:00:36,720 ‫So that's pretty much it. 12 00:00:36,730 --> 00:00:38,410 ‫There's not much more than that. 13 00:00:38,590 --> 00:00:42,130 ‫And we will build these basic functionalities in this chapter. 14 00:00:42,130 --> 00:00:52,870 ‫So let's go ahead and create a new project, and let's call that Fruit Ninja clone and let's create 15 00:00:52,870 --> 00:00:53,800 ‫the project. 16 00:00:54,370 --> 00:00:57,820 ‫In my case, I call it V one. 17 00:00:58,450 --> 00:00:59,650 ‫You don't have to do that. 18 00:00:59,770 --> 00:01:04,510 ‫And that's just because I already have a clone somewhere under my project. 19 00:01:04,750 --> 00:01:06,160 ‫All right, so there we are. 20 00:01:06,820 --> 00:01:10,720 ‫Let's go ahead and first of all, change the background of our camera. 21 00:01:10,720 --> 00:01:12,940 ‫So I don't want to have a skybox. 22 00:01:12,940 --> 00:01:18,160 ‫I'm just going to use a solid color and I'm going to change that color to. 23 00:01:19,330 --> 00:01:21,370 ‫Let's go ahead and pick a color. 24 00:01:25,290 --> 00:01:28,980 ‫And let's go with a brownish color. 25 00:01:30,360 --> 00:01:34,080 ‫Where light brown a bash almost great. 26 00:01:34,080 --> 00:01:35,670 ‫So that's our starting point. 27 00:01:35,880 --> 00:01:42,810 ‫And now let's import the assets that we have because there are some assets which you can download which 28 00:01:42,810 --> 00:01:49,440 ‫are attached to this lecture and let's go ahead and drag all of them into our game. 29 00:01:49,440 --> 00:01:54,360 ‫So there is banana cut, banana blend bomb, orange cut, orange blend. 30 00:01:54,840 --> 00:02:00,900 ‫So all these blend files which are blender files and let's drag them into our game. 31 00:02:01,770 --> 00:02:02,280 ‫All right. 32 00:02:02,280 --> 00:02:09,840 ‫So now, as you can see, the assets were created for us directly and the materials as well. 33 00:02:10,710 --> 00:02:19,620 ‫Let's start off by creating a folder for them, which I'm going to call models, and I'm going to drag 34 00:02:19,620 --> 00:02:20,760 ‫all of them in there. 35 00:02:24,520 --> 00:02:25,530 ‫Even materials. 36 00:02:25,540 --> 00:02:27,250 ‫So let's drag all of it in there. 37 00:02:28,060 --> 00:02:28,420 ‫All right. 38 00:02:28,420 --> 00:02:30,890 ‫So first of all, let's start with an orange. 39 00:02:30,910 --> 00:02:32,770 ‫Let's drag it into our game. 40 00:02:32,830 --> 00:02:35,930 ‫As you can see, this is the orange and that's how big it would be. 41 00:02:35,950 --> 00:02:40,480 ‫So maybe it's a good choice to reduce its size. 42 00:02:41,350 --> 00:02:49,120 ‫So here, go to model and change the scale factor to 0.3 and then hit apply at the bottom. 43 00:02:49,120 --> 00:02:53,380 ‫As you can see now, it's much smaller and that will be a much better size. 44 00:02:53,380 --> 00:02:55,120 ‫Otherwise it's way too big. 45 00:02:55,150 --> 00:03:02,890 ‫Now let's do the same thing with our cut orange or orange cut 0.3 and then hit apply. 46 00:03:03,520 --> 00:03:07,810 ‫Now let's drag the orange cut in there as well, actually in here. 47 00:03:07,990 --> 00:03:11,110 ‫So now you might wonder what that means. 48 00:03:11,110 --> 00:03:17,500 ‫Well, we have one version, which is the complete orange, which is uncut, and then there is the cut 49 00:03:17,500 --> 00:03:25,840 ‫version, which is the one that will be created as soon as we slice through or swipe through an orange. 50 00:03:26,230 --> 00:03:27,970 ‫And it's two pieces. 51 00:03:27,970 --> 00:03:32,740 ‫So it contains two pieces which will then fall off to different directions. 52 00:03:32,740 --> 00:03:38,770 ‫So we will get this slightly feeling of slicing an orange or in general or slicing a fruit. 53 00:03:40,930 --> 00:03:41,290 ‫All right. 54 00:03:41,290 --> 00:03:46,930 ‫So let's get rid of the orange cut again, because that was just for demonstration purposes. 55 00:03:47,650 --> 00:03:50,280 ‫So let's delete the orange cut here. 56 00:03:50,290 --> 00:03:54,400 ‫Now, the orange has an animator already, as you can see. 57 00:03:54,400 --> 00:03:57,610 ‫It doesn't have a controller, though, and we won't need that for now. 58 00:03:57,610 --> 00:03:58,720 ‫So let's get rid of it. 59 00:03:59,170 --> 00:04:00,330 ‫Remove component. 60 00:04:00,340 --> 00:04:04,840 ‫Now we only have the mesh render the mesh filter transform component. 61 00:04:04,840 --> 00:04:10,000 ‫And what we will need to add is a physics to the rigid body. 62 00:04:10,030 --> 00:04:14,960 ‫Now you might wonder why a 2D physics body and why not 3D? 63 00:04:14,980 --> 00:04:19,870 ‫Well, that's because I only want this orange to move into 2D room. 64 00:04:19,870 --> 00:04:23,650 ‫So it shouldn't move to the it should move to the top, to the left, to the right. 65 00:04:23,650 --> 00:04:28,270 ‫But it shouldn't go in a deep level, so it shouldn't go in the 3D level. 66 00:04:28,300 --> 00:04:31,900 ‫That's why Rigid Body 2D is the right rigid body for us. 67 00:04:32,980 --> 00:04:40,990 ‫Now let's add another component, which is a physics 2D circle collider because as you might guess, 68 00:04:42,100 --> 00:04:44,740 ‫our orange has the form of a circle. 69 00:04:44,740 --> 00:04:48,700 ‫So as you can see now, this is going to be the 2D collider. 70 00:04:49,150 --> 00:04:52,030 ‫You can even see it better if we go to the 2D room. 71 00:04:52,030 --> 00:04:58,840 ‫Now, you see that green little line surrounding our orange is going to be the Circle Collider. 72 00:04:59,590 --> 00:05:04,330 ‫Now the radius of the collider, you can find it here and you could change that. 73 00:05:04,330 --> 00:05:07,750 ‫As you can see here, making it bigger or smaller. 74 00:05:07,750 --> 00:05:12,460 ‫If you want to make the game easier, you would make it bigger or you could even in general scale up 75 00:05:12,460 --> 00:05:13,360 ‫the whole model. 76 00:05:13,360 --> 00:05:16,330 ‫But 0.3 is the right radius for us. 77 00:05:18,310 --> 00:05:22,450 ‫Now we could even get rid of this offset because it was so minimal. 78 00:05:22,720 --> 00:05:24,010 ‫It's just something that unity. 79 00:05:24,040 --> 00:05:25,840 ‫Unity does sometimes. 80 00:05:27,280 --> 00:05:30,970 ‫Well, now the rigid body is all right. 81 00:05:30,970 --> 00:05:32,110 ‫We will need a mass. 82 00:05:32,110 --> 00:05:34,140 ‫We will need a gravity scale. 83 00:05:34,150 --> 00:05:36,940 ‫But what we should change here is the collision detection. 84 00:05:36,970 --> 00:05:40,750 ‫This is fine in most games, but we want it to be super fast. 85 00:05:40,750 --> 00:05:43,390 ‫So we want to have a very quick detection here. 86 00:05:43,390 --> 00:05:49,210 ‫Collision detection, so continuous is the better choice for us because it's a very quick game in which 87 00:05:49,210 --> 00:05:52,120 ‫we slice the orange and it should react very quickly. 88 00:05:52,660 --> 00:05:55,330 ‫Now let's save that orange as a prefab. 89 00:05:55,330 --> 00:06:04,150 ‫So let's create a new prefab folder, folder prefab and drag the orange in here. 90 00:06:04,450 --> 00:06:07,360 ‫And while we're at it, let's save the scene as well. 91 00:06:07,360 --> 00:06:10,390 ‫So save scenes and let's call that one main. 92 00:06:11,680 --> 00:06:15,670 ‫Then we will need a script folder because we're going to work with script. 93 00:06:15,670 --> 00:06:22,180 ‫So let's create that as well script and let's go in there, create a new script. 94 00:06:22,180 --> 00:06:25,540 ‫I see sharp script to be precise and call it fruit. 95 00:06:25,780 --> 00:06:27,040 ‫Now let's open it up. 96 00:06:27,760 --> 00:06:28,720 ‫All right, in here. 97 00:06:28,720 --> 00:06:33,130 ‫What we want to do is we want to create a sliced fruit. 98 00:06:33,340 --> 00:06:35,320 ‫So we will need a sliced fruit. 99 00:06:35,320 --> 00:06:41,920 ‫Prefab for that, which means as soon as we cut our orange, we want to have the orange pieces. 100 00:06:41,920 --> 00:06:47,260 ‫So the two to the two parts of our orange, which will fall off into different directions. 101 00:06:47,260 --> 00:06:49,330 ‫And that's something that we need to set up here. 102 00:06:49,330 --> 00:06:52,930 ‫And that's not just for the orange is for any fruit that we're going to work with. 103 00:06:52,930 --> 00:07:03,580 ‫So let's create a public game object called sliced fruit prefab because we will need the prefab of the 104 00:07:03,580 --> 00:07:11,020 ‫sliced fruit, which is, for example, the sliced orange, and then we'll need a method which slices 105 00:07:11,020 --> 00:07:11,530 ‫the fruit. 106 00:07:11,530 --> 00:07:15,640 ‫So I'm going to call it create sliced fruit. 107 00:07:18,070 --> 00:07:23,200 ‫And what I want to do is simply instantiate the sliced fruit. 108 00:07:23,200 --> 00:07:23,680 ‫So. 109 00:07:23,740 --> 00:07:24,700 ‫Sliced fruit. 110 00:07:24,700 --> 00:07:25,450 ‫Prefab. 111 00:07:25,840 --> 00:07:27,100 ‫At which position? 112 00:07:27,190 --> 00:07:31,150 ‫Well, at the same position as our oranges. 113 00:07:31,150 --> 00:07:35,170 ‫And then transform the rotation. 114 00:07:35,170 --> 00:07:38,830 ‫So also the same rotation that we have now. 115 00:07:38,830 --> 00:07:42,040 ‫The next thing we need to do is, of course to destroy the whole orange. 116 00:07:42,040 --> 00:07:47,770 ‫So our game object, which is the game object that contains the fruit script or holds the fruit script. 117 00:07:49,480 --> 00:07:50,080 ‫All right. 118 00:07:50,080 --> 00:07:56,770 ‫Now let's go into unity again because we don't have the prefab yet, so we don't have the sliced prefab 119 00:07:56,770 --> 00:07:56,950 ‫yet. 120 00:07:56,950 --> 00:07:58,960 ‫Of course, we only have the orange prefab. 121 00:07:58,960 --> 00:08:04,690 ‫So let's go back to our model and let's drag the orange cut again into our game. 122 00:08:04,690 --> 00:08:11,770 ‫Now, the orange cut consists of two different objects, as you can see here. 123 00:08:11,770 --> 00:08:18,310 ‫This is just a folder pretty much which contains our orange cut left and our orange cut right. 124 00:08:18,310 --> 00:08:21,190 ‫So there are two pieces of the orange. 125 00:08:21,190 --> 00:08:28,180 ‫We want them to fly into different directions as soon as this create sliced fruit method is called. 126 00:08:28,180 --> 00:08:30,340 ‫So let's go ahead and do that. 127 00:08:30,340 --> 00:08:33,400 ‫Let's select those two cut pieces and add a component. 128 00:08:33,400 --> 00:08:38,860 ‫And now we're going to add a rigid body, not a rigid body, 2D, because we don't only want to fold 129 00:08:38,860 --> 00:08:41,740 ‫them off to the side, we also want them to slightly rotate. 130 00:08:41,740 --> 00:08:47,170 ‫So it looks a little bit more realistic because the fruit won't always fall off perfectly to each sides. 131 00:08:47,200 --> 00:08:49,360 ‫It will also rotate a little bit. 132 00:08:49,360 --> 00:08:52,960 ‫And that's something that we need to do with a rigid body 3D. 133 00:08:53,200 --> 00:08:58,600 ‫Now, about the collision detection, we can leave it as discrete because those pieces will not be as 134 00:08:58,600 --> 00:09:00,880 ‫important in terms of collision detection. 135 00:09:00,880 --> 00:09:05,890 ‫We don't want them to be sliced again, but we still need a physics. 136 00:09:06,670 --> 00:09:07,120 ‫Box. 137 00:09:07,120 --> 00:09:13,270 ‫Collider Well, in this case, we're going to use a box collider now here, of course, a box collider, 138 00:09:13,270 --> 00:09:16,850 ‫not a box collider to DD as our rigid body is a 3D object. 139 00:09:16,870 --> 00:09:22,000 ‫So now that we have that, let's drag them into our prefabs folder. 140 00:09:23,320 --> 00:09:31,390 ‫So let's actually drag the orange cut as a prefab into our prefabs folder because we want to have both 141 00:09:31,390 --> 00:09:34,150 ‫pieces and we want to have one prefab. 142 00:09:35,440 --> 00:09:38,260 ‫All right, so now we can get rid of our orange cut. 143 00:09:38,590 --> 00:09:40,390 ‫Delete that from here. 144 00:09:40,480 --> 00:09:41,910 ‫Let's save our scene as well. 145 00:09:41,920 --> 00:09:48,100 ‫So now the orange like this orange and the prefab, they both need to have the script that we've just 146 00:09:48,100 --> 00:09:48,550 ‫created. 147 00:09:48,550 --> 00:09:49,870 ‫So let's add the component. 148 00:09:49,870 --> 00:09:52,690 ‫Let's add the script called Fruit. 149 00:09:52,720 --> 00:09:56,710 ‫Now, of course, you see here we will need a sliced fruit prefab. 150 00:09:56,710 --> 00:10:00,520 ‫And it's perfect because orange cut is a sliced fruit prefab. 151 00:10:00,700 --> 00:10:05,790 ‫So let's drag that in there and let's apply that to our prefab orange. 152 00:10:05,800 --> 00:10:07,180 ‫Otherwise it won't have the script. 153 00:10:07,180 --> 00:10:09,820 ‫So you can see the prefab has a script that's really important. 154 00:10:10,240 --> 00:10:11,860 ‫All right, now let's that's done. 155 00:10:11,860 --> 00:10:15,010 ‫Let's try to get that happening. 156 00:10:15,010 --> 00:10:22,450 ‫So let's try to create a sliced fruit and we can use the update method for user input. 157 00:10:22,450 --> 00:10:26,830 ‫So if input dot get key down. 158 00:10:26,830 --> 00:10:32,440 ‫So as soon as the user presses a key and let's just do key code space. 159 00:10:32,650 --> 00:10:39,160 ‫So as soon as somebody presses the space key, let's just call this create sliced fruit method. 160 00:10:39,580 --> 00:10:41,380 ‫All right, so let's save that script. 161 00:10:41,380 --> 00:10:42,970 ‫Let's go back to Unity. 162 00:10:43,090 --> 00:10:49,330 ‫And now it's important that we have an orange in our hierarchy, because otherwise we don't have any 163 00:10:49,330 --> 00:10:54,220 ‫oranges to test that on as we don't have any spawn or yet. 164 00:10:54,220 --> 00:10:59,530 ‫So we will create spanners, which will spawn fruits and shoot them up the air so that we can slice 165 00:10:59,530 --> 00:10:59,950 ‫them. 166 00:10:59,950 --> 00:11:01,600 ‫But now we don't have that yet. 167 00:11:01,600 --> 00:11:06,970 ‫So let's make sure that an orange is in our game and let's start the game now. 168 00:11:06,970 --> 00:11:11,350 ‫Actually, I was too slow, so let's drag it up a little bit. 169 00:11:11,440 --> 00:11:15,760 ‫Let's go into the 2D room and let's pull it up. 170 00:11:15,760 --> 00:11:19,690 ‫So I have enough, enough time and let's maximize on play. 171 00:11:21,900 --> 00:11:22,950 ‫And the press, the key. 172 00:11:22,950 --> 00:11:26,930 ‫And as you can see, the sliced fruit was there and that's all good. 173 00:11:26,940 --> 00:11:28,950 ‫But of course, it doesn't look very nice. 174 00:11:28,950 --> 00:11:32,820 ‫It should rotate somehow or should move towards different directions. 175 00:11:32,910 --> 00:11:39,810 ‫And that's what we can get in our create sliced fruit because we need to add a rigid body here, which 176 00:11:39,810 --> 00:11:41,100 ‫does an explosion. 177 00:11:41,100 --> 00:11:44,100 ‫And in order to do that, we have to do the following thing. 178 00:11:44,100 --> 00:11:46,440 ‫So let's create a rigid body. 179 00:11:46,740 --> 00:11:51,840 ‫And in this case, we need an array of rigid bodies because we have two rigid bodies that we are talking 180 00:11:51,840 --> 00:11:52,200 ‫to. 181 00:11:52,200 --> 00:11:57,240 ‫So the one of the left sliced piece and the one of the right sliced piece, and I'm just going to call 182 00:11:57,240 --> 00:12:02,430 ‫it RB s or rigid bodies on sliced. 183 00:12:03,660 --> 00:12:08,400 ‫And in order to initialize it, we need the game object to initialize it with. 184 00:12:08,400 --> 00:12:14,490 ‫So let's create a game object and let's call it instance of. 185 00:12:16,640 --> 00:12:23,360 ‫Now the instantiate method needs to get a game object in front of it so that this will now not only 186 00:12:23,360 --> 00:12:30,320 ‫instantiate the game object, but will also create a game object which is saved in Inst. 187 00:12:30,590 --> 00:12:43,490 ‫Now we can use that inst to now go ahead and transform the get component component in children and here 188 00:12:43,490 --> 00:12:44,420 ‫to rigid body. 189 00:12:44,420 --> 00:12:48,860 ‫So I want to have the rigid body of each of the two sliced pieces. 190 00:12:49,250 --> 00:12:55,610 ‫And here it's complaining because it's not a component in children, but it's components in children, 191 00:12:55,610 --> 00:12:57,770 ‫because here we have an array of rigid bodies. 192 00:12:57,770 --> 00:12:58,850 ‫So it's plural. 193 00:12:58,970 --> 00:13:02,630 ‫Now let's go through all of the different pieces that we have. 194 00:13:02,630 --> 00:13:11,660 ‫So all of these different sliced orange pieces and take the rigid body called are in RB on sliced. 195 00:13:11,750 --> 00:13:22,190 ‫So we're going through this array here and for each of the pieces we change the transform rotation with 196 00:13:22,190 --> 00:13:23,360 ‫a random rotation. 197 00:13:23,360 --> 00:13:27,260 ‫So we just create a random row tation. 198 00:13:27,260 --> 00:13:32,870 ‫And that's beautiful because you see the random method has a property called rotation which will just 199 00:13:32,870 --> 00:13:34,100 ‫give us a random rotation. 200 00:13:34,100 --> 00:13:38,180 ‫We don't even need to write a method here to do any random rotations here. 201 00:13:38,180 --> 00:13:40,840 ‫So that's pretty cool with with the random class. 202 00:13:40,850 --> 00:13:46,760 ‫Now, next, we want to add an explosion so that the pieces go to different directions and they just 203 00:13:46,760 --> 00:13:48,740 ‫get blasted to the different directions. 204 00:13:48,740 --> 00:13:54,470 ‫And as you can see here, our rigid body has several add methods, add force, add relative force, 205 00:13:54,470 --> 00:13:55,880 ‫add explosion force. 206 00:13:55,880 --> 00:13:56,360 ‫All right. 207 00:13:56,360 --> 00:13:58,370 ‫That's exactly the one that we want to have. 208 00:13:58,370 --> 00:13:59,750 ‫So let's use that. 209 00:13:59,840 --> 00:14:07,700 ‫As you can see, there are multiple overloads and we're going to use this one here just with a explosion 210 00:14:07,700 --> 00:14:08,120 ‫force. 211 00:14:08,120 --> 00:14:11,540 ‫So how powerful should the explosion force be then? 212 00:14:11,540 --> 00:14:13,220 ‫The explosion position? 213 00:14:13,520 --> 00:14:16,970 ‫So the position from where the explosion should start. 214 00:14:16,970 --> 00:14:23,390 ‫And then finally the explosion radius and the value for explosion force should be random. 215 00:14:23,390 --> 00:14:25,970 ‫It shouldn't always be the same kind of explosion. 216 00:14:26,000 --> 00:14:27,830 ‫It should not always look the same. 217 00:14:27,830 --> 00:14:32,990 ‫So sometimes it should fly away further, sometimes less far, and so forth. 218 00:14:33,350 --> 00:14:38,900 ‫So let's create a random thought range. 219 00:14:39,740 --> 00:14:44,090 ‫And the two values I want to use here are 501,000. 220 00:14:44,420 --> 00:14:50,240 ‫So I just want to have a explosion force of between 501,000, which should be random. 221 00:14:50,240 --> 00:14:54,230 ‫Then the explosion position should be transformed that position. 222 00:14:54,230 --> 00:14:57,380 ‫So the position of the game object from which we exploded. 223 00:14:57,380 --> 00:15:00,980 ‫So it's in this case our orange, so the center of our orange. 224 00:15:01,130 --> 00:15:04,100 ‫And then finally what should be the explosion radius? 225 00:15:04,100 --> 00:15:05,750 ‫And I'm just going to enter five. 226 00:15:05,780 --> 00:15:09,440 ‫That's just the value that I have tested and works well here. 227 00:15:09,440 --> 00:15:21,590 ‫So let's use five F now let's save that again and let's go back to Unity and now let's press the key. 228 00:15:21,590 --> 00:15:24,860 ‫And as you can see, our fruit falls off pretty cool. 229 00:15:24,860 --> 00:15:30,680 ‫Now, if we duplicate the orange and reposition each orange it a little bit. 230 00:15:30,680 --> 00:15:33,290 ‫So let's change the values here. 231 00:15:34,250 --> 00:15:40,790 ‫Then you will see how cool our little explosion looks. 232 00:15:42,260 --> 00:15:42,800 ‫All right. 233 00:15:42,800 --> 00:15:45,230 ‫And now finally, this one here as well. 234 00:15:45,230 --> 00:15:46,910 ‫So now let's start the game again. 235 00:15:48,520 --> 00:15:54,310 ‫And you might see the rotation is random and the explosion itself is random as well. 236 00:15:54,310 --> 00:15:58,770 ‫So they fly off to different directions, different to powerful and so forth. 237 00:15:58,780 --> 00:16:00,760 ‫So I think this looks pretty cool already. 238 00:16:00,760 --> 00:16:07,180 ‫So you see, we do great explosions and yeah, I think that's good for this video. 239 00:16:07,180 --> 00:16:13,900 ‫And the next video we're going to check how to spawn those fruits because now we have to drag them into 240 00:16:13,900 --> 00:16:16,180 ‫our scene and that's not the best solution. 241 00:16:16,180 --> 00:16:18,700 ‫So let's save the scene as usual. 242 00:16:18,700 --> 00:16:21,970 ‫Don't forget to save the scene and see you in the next video.