1 00:00:03,950 --> 00:00:04,910 ‫Welcome back. 2 00:00:04,940 --> 00:00:12,710 ‫Now, if we start the game and we check one behavior, which is if we cut our oranges, they will also 3 00:00:12,710 --> 00:00:14,210 ‫stay in the game infinitely. 4 00:00:14,210 --> 00:00:16,190 ‫So we have taken care of our oranges. 5 00:00:16,190 --> 00:00:18,770 ‫They will get deleted after a certain amount of time. 6 00:00:18,770 --> 00:00:22,640 ‫But our cut oranges not so let's change that. 7 00:00:22,640 --> 00:00:27,110 ‫Let's go into our script and in this case, into our fruit script. 8 00:00:27,110 --> 00:00:34,700 ‫So here destroy insert dot game object after 5 seconds. 9 00:00:34,910 --> 00:00:43,110 ‫So this instantiated game object, which is our created slice sliced fruit, will be deleted after 5 10 00:00:43,110 --> 00:00:43,610 ‫seconds. 11 00:00:44,420 --> 00:00:46,250 ‫So let's have a look, if that's true now. 12 00:00:51,370 --> 00:00:56,920 ‫Now I'm gonna slice and let's see if those orange cut game objects get deleted after 5 seconds. 13 00:00:57,490 --> 00:00:59,290 ‫And as you can see, they are gone. 14 00:01:00,880 --> 00:01:02,290 ‫Those things are very important. 15 00:01:02,470 --> 00:01:06,430 ‫If you don't take care of those things, you might run into performance issues. 16 00:01:06,430 --> 00:01:14,050 ‫And that's not that big of an issue with a PC, which has plenty of powerful hardware, but it's a problem 17 00:01:14,050 --> 00:01:14,860 ‫with smartphones. 18 00:01:14,860 --> 00:01:20,140 ‫So really be careful with creation of game objects and deleting them. 19 00:01:21,100 --> 00:01:21,430 ‫All right. 20 00:01:21,430 --> 00:01:22,870 ‫Next, let's create the blade. 21 00:01:22,870 --> 00:01:27,280 ‫So let's create an empty game object and call that one blade. 22 00:01:28,330 --> 00:01:34,990 ‫Now let's reset the transform position and let's add a component, which is a trail renderer. 23 00:01:34,990 --> 00:01:37,030 ‫So there's the thing called trailer renderer. 24 00:01:37,060 --> 00:01:38,980 ‫Let's just use that. 25 00:01:38,980 --> 00:01:41,290 ‫Save the script and check it out. 26 00:01:42,580 --> 00:01:50,710 ‫And if we move around and let's play around with the X and Y value, and as you can see now, it creates 27 00:01:50,710 --> 00:01:58,060 ‫this magenta, which means we don't have the correct material for our blade yet, so let's create a 28 00:01:58,060 --> 00:01:59,200 ‫material for that. 29 00:01:59,920 --> 00:02:05,320 ‫Now let's go into our assets folder and create a new folder in order to create a material for it. 30 00:02:05,320 --> 00:02:09,760 ‫So material and actually materials, plural. 31 00:02:10,390 --> 00:02:16,930 ‫And in here, I want to create a new material and I'm going to call it Blade. 32 00:02:18,730 --> 00:02:28,210 ‫Now, it needs a color, of course, and I'm going to take a light blue, actually, maybe a bit more 33 00:02:28,210 --> 00:02:30,550 ‫powerful just like that. 34 00:02:31,360 --> 00:02:34,690 ‫You can take any color that you like, so I'm just going to take this blue. 35 00:02:34,720 --> 00:02:38,200 ‫Now let's check out our blade and it needs here material. 36 00:02:38,200 --> 00:02:42,460 ‫So click on materials and it says there is one material with none in here. 37 00:02:42,460 --> 00:02:44,710 ‫So there's no material in the element zero. 38 00:02:44,800 --> 00:02:46,300 ‫Let's drag that in there. 39 00:02:47,500 --> 00:02:57,100 ‫And if we test that now and move our blade back and forth, we will see that there is our blue material. 40 00:02:57,100 --> 00:03:02,380 ‫Now what we want is that our trailer renderer follows along our mouse and there is a functionality that 41 00:03:02,380 --> 00:03:02,860 ‫does that. 42 00:03:02,860 --> 00:03:05,290 ‫So it will check out the vector three values. 43 00:03:05,290 --> 00:03:06,310 ‫So a3d value. 44 00:03:06,310 --> 00:03:12,340 ‫In our world, of course, our game is a 2D game, but still the mouse is within a 3D world and we're 45 00:03:12,340 --> 00:03:15,460 ‫just going to use the 2D components that are relevant for us. 46 00:03:15,760 --> 00:03:16,420 ‫All right. 47 00:03:17,500 --> 00:03:23,770 ‫Now, if we look at our trade renderer, we see that it stays on the game for quite a while. 48 00:03:23,770 --> 00:03:25,480 ‫And that has to do with this time. 49 00:03:25,480 --> 00:03:28,540 ‫And let's just change that to 0.2. 50 00:03:28,540 --> 00:03:35,050 ‫So it will only be alive for 0.2 seconds, which means it will be a little trail, but it will not be 51 00:03:35,050 --> 00:03:36,490 ‫a very long trail here. 52 00:03:36,580 --> 00:03:37,600 ‫Let's test that. 53 00:03:37,840 --> 00:03:44,050 ‫Let's run it again and change the position of our X value. 54 00:03:44,050 --> 00:03:48,280 ‫As you can see, it doesn't stay alive for quite a while. 55 00:03:48,280 --> 00:03:51,130 ‫It only very only has a very short life. 56 00:03:51,310 --> 00:03:58,480 ‫And of course, it's also way too wide because we shouldn't have this huge cut or this huge blade look 57 00:03:58,480 --> 00:03:59,170 ‫in our game. 58 00:03:59,170 --> 00:04:03,640 ‫So let's go with 0.1 and let's see how that looks. 59 00:04:05,230 --> 00:04:06,640 ‫Let's play around with the X value. 60 00:04:06,640 --> 00:04:08,910 ‫And there you see, this is how it could look like. 61 00:04:08,920 --> 00:04:13,780 ‫So this is how our our trail will look like. 62 00:04:14,980 --> 00:04:20,200 ‫Now, in order to have a nicer look, so actually I'm going to make the game scene a little bit bigger 63 00:04:20,200 --> 00:04:22,110 ‫so we can see it a bit better. 64 00:04:22,120 --> 00:04:33,760 ‫So I want to change the looks of my trail because currently what it does, it is always 0.1 and we want 65 00:04:33,760 --> 00:04:34,810 ‫to change that. 66 00:04:36,670 --> 00:04:38,650 ‫So let's add a key now. 67 00:04:38,650 --> 00:04:45,430 ‫Let's drag the center here to the top and let's add another key here at the end and let's drag it to 68 00:04:45,430 --> 00:04:46,090 ‫the bottom. 69 00:04:46,180 --> 00:04:48,340 ‫So let's have a look what that means. 70 00:04:48,910 --> 00:04:55,120 ‫So if we move our X position, as you can see, it looks more like a cut with a knife. 71 00:04:55,810 --> 00:05:02,260 ‫You can only see it very quickly, but it will make much more sense as soon as we have our mouse functionality 72 00:05:02,260 --> 00:05:03,070 ‫implemented. 73 00:05:03,850 --> 00:05:04,270 ‫Great. 74 00:05:04,270 --> 00:05:12,040 ‫So next our blade will need a rigid body so that it can move and has a physical impact into the game. 75 00:05:12,040 --> 00:05:16,750 ‫Because what we want to have is of course, if we slice with the blade, which will follow along with 76 00:05:16,750 --> 00:05:22,150 ‫our mouse, if we slice through an object, we want that object to be impacted. 77 00:05:22,150 --> 00:05:30,730 ‫And in order to do that, let's go ahead and add a rigid body to DX and it should be kinematic. 78 00:05:30,730 --> 00:05:37,540 ‫What that does is it tells our blade that it shouldn't be influenced by other objects, but it will 79 00:05:37,540 --> 00:05:39,400 ‫still influence other objects. 80 00:05:39,400 --> 00:05:46,210 ‫So for example, it will still influence our fruits, but the fruits will not drag or push off our blade. 81 00:05:49,290 --> 00:05:54,330 ‫And the collision detection should be continuous here as well because we want to have a very fast collision 82 00:05:54,330 --> 00:05:54,780 ‫here. 83 00:05:56,490 --> 00:05:56,790 ‫All right. 84 00:05:56,790 --> 00:06:02,310 ‫Next, we want to have a script which, of course, takes care of this beautiful behavior that we've 85 00:06:02,310 --> 00:06:06,930 ‫just seen just with the mouse so that it doesn't follow along the X and Y position that we have set 86 00:06:06,930 --> 00:06:11,220 ‫here in our transform component, but it actually follows our mouse. 87 00:06:11,370 --> 00:06:17,130 ‫So let's call that script blade and let's open that script up. 88 00:06:21,570 --> 00:06:27,810 ‫And in here we will use the awake method instead of our start method. 89 00:06:27,810 --> 00:06:37,290 ‫And in here we will actually create a rigid body, private or B or rigid body, which I'm going to call 90 00:06:37,290 --> 00:06:48,930 ‫RB and let's initialize that RB with get component rigid body to DX. 91 00:06:49,980 --> 00:06:56,100 ‫What that will do is it will simply take the rigid body component from the blade itself because the 92 00:06:56,100 --> 00:06:57,870 ‫blade contains that script. 93 00:06:57,870 --> 00:06:59,850 ‫So it will take that rigid body here. 94 00:07:00,660 --> 00:07:05,850 ‫Next, we need a method which will take care of our blade following our mouse. 95 00:07:05,850 --> 00:07:11,760 ‫So let's create a new method, private void set, relate to mouse. 96 00:07:12,600 --> 00:07:19,860 ‫So this method will take care of setting the blade to the position of our mouse and we simply change 97 00:07:19,860 --> 00:07:20,730 ‫the rigid body. 98 00:07:20,730 --> 00:07:31,650 ‫So the RB position to camera dot main that screen to world. 99 00:07:33,950 --> 00:07:39,020 ‫Point transforms position from screen space into world space. 100 00:07:39,020 --> 00:07:41,030 ‫So that's exactly what we want here. 101 00:07:41,150 --> 00:07:47,120 ‫And now we simply say the input should be our mouse position. 102 00:07:47,120 --> 00:07:53,720 ‫So wherever our mouse is in comparison to the world, that's where we want the rigid body to be or the 103 00:07:53,720 --> 00:07:55,310 ‫rigid body of our blade to be. 104 00:07:55,310 --> 00:07:57,050 ‫And that will give us the slide. 105 00:07:57,050 --> 00:07:59,270 ‫Look, we will see that in a second. 106 00:07:59,270 --> 00:08:04,670 ‫And of course, in our update method, we will constantly call the set blade to mouse in order to keep 107 00:08:04,670 --> 00:08:08,630 ‫it up to date so that it always follows the follows along our mouse. 108 00:08:08,630 --> 00:08:15,260 ‫So it will change the position of the rigid body with every single frame based on where our mouse is. 109 00:08:15,800 --> 00:08:18,380 ‫All right, so let's go back to the Blade script. 110 00:08:18,380 --> 00:08:20,540 ‫And here there is a little thing that we need to solve. 111 00:08:20,540 --> 00:08:24,380 ‫So in some cases it might work for you. 112 00:08:24,380 --> 00:08:29,240 ‫So if you start the game, you will already see the mouse being followed. 113 00:08:29,240 --> 00:08:36,500 ‫But in other cases it will not work because the mouse position itself is a 3D point, but our world 114 00:08:36,500 --> 00:08:40,700 ‫is a 2D world, so now it doesn't always calculate it correctly. 115 00:08:40,700 --> 00:08:48,290 ‫So in order to fix that, what we will need to do is we create a new variable called mouse position 116 00:08:48,320 --> 00:08:55,460 ‫or mouse pose, and we set that to input mouse position, which is the one here. 117 00:08:56,120 --> 00:08:57,230 ‫It's the same position. 118 00:08:57,230 --> 00:09:04,010 ‫But now what we do is we set the Z value of our mouse position to the value of ten. 119 00:09:04,670 --> 00:09:08,210 ‫So that means that this is going to be the distance 120 00:09:11,750 --> 00:09:15,260 ‫of ten units from the camera. 121 00:09:21,170 --> 00:09:28,700 ‫And now what we can do here is we don't hand over the input mouse position, but we hand over the mouse 122 00:09:28,700 --> 00:09:29,390 ‫pose. 123 00:09:30,440 --> 00:09:32,030 ‫Now let's save that. 124 00:09:32,060 --> 00:09:33,650 ‫Let's go back to unity. 125 00:09:33,650 --> 00:09:37,940 ‫And by the way, and this ten here actually is this minus ten here. 126 00:09:37,940 --> 00:09:41,990 ‫So we go ten further in in order to get the correct position. 127 00:09:42,860 --> 00:09:43,490 ‫All right. 128 00:09:44,360 --> 00:09:51,980 ‫So let's try that again and now actually get a null pointer exception. 129 00:09:53,780 --> 00:09:58,730 ‫And here, of course, judge, that it should be a week. 130 00:09:58,980 --> 00:09:59,510 ‫Okay. 131 00:09:59,510 --> 00:10:01,220 ‫So now let's check that out. 132 00:10:01,490 --> 00:10:03,260 ‫Let's start the game again. 133 00:10:07,340 --> 00:10:08,460 ‫And let's see. 134 00:10:08,480 --> 00:10:09,320 ‫There you are. 135 00:10:09,920 --> 00:10:12,920 ‫Our plate is being followed. 136 00:10:12,920 --> 00:10:15,200 ‫So this is the blade effect that we have now. 137 00:10:15,200 --> 00:10:21,110 ‫So if we swipe through, we, of course, now want to delete or we want to cut the fruits. 138 00:10:21,320 --> 00:10:22,670 ‫So let's do that. 139 00:10:23,120 --> 00:10:25,070 ‫All right, so now let's cut the fruits. 140 00:10:25,070 --> 00:10:28,460 ‫Let's go into our fruit script. 141 00:10:28,460 --> 00:10:35,900 ‫And in here we are going to add an on trigger enter event on trigger enter to DX. 142 00:10:35,930 --> 00:10:43,580 ‫So we call this method and we say whenever we collide with the blade, we want to go ahead and create 143 00:10:43,910 --> 00:10:44,720 ‫sliced fruits. 144 00:10:44,720 --> 00:10:49,550 ‫So what that will do is go ahead and cut our fruit. 145 00:10:49,550 --> 00:10:51,500 ‫So let's create a blade object here. 146 00:10:51,500 --> 00:11:03,860 ‫Blade B we take the collision dot get component and we call it blade or or actually we get the blade 147 00:11:03,860 --> 00:11:04,280 ‫itself. 148 00:11:04,280 --> 00:11:11,570 ‫So if there is a component called blade with the object that we collided with, then create an object 149 00:11:11,570 --> 00:11:20,090 ‫called B, which is of type blade and then we check if there is no B, then just return. 150 00:11:20,480 --> 00:11:23,540 ‫And what that will do is, well, it will do nothing any more. 151 00:11:23,540 --> 00:11:31,490 ‫So it will just get out of the call of this on trigger enter event, but otherwise go ahead and create 152 00:11:32,000 --> 00:11:33,770 ‫a sliced fruit. 153 00:11:35,120 --> 00:11:37,580 ‫So now we won't do that in the update method anymore. 154 00:11:37,580 --> 00:11:38,720 ‫So let's get rid of that. 155 00:11:39,020 --> 00:11:41,450 ‫We will do it however in the on trigger enter. 156 00:11:41,450 --> 00:11:45,980 ‫So we will create sliced fruit whenever we collide with the blade. 157 00:11:45,980 --> 00:11:48,860 ‫And if we don't collide with the blade, then just return it to nothing. 158 00:11:49,190 --> 00:11:50,810 ‫So let's check that out. 159 00:11:51,080 --> 00:11:52,370 ‫Let's run our code. 160 00:11:52,370 --> 00:11:58,820 ‫Well, actually, before we try that, let's add a collider of more to our blade, because currently 161 00:11:58,820 --> 00:12:00,200 ‫it doesn't have a collider. 162 00:12:00,290 --> 00:12:04,280 ‫So let's create a collider going. 163 00:12:04,280 --> 00:12:05,630 ‫Going to add a. 164 00:12:08,940 --> 00:12:10,320 ‫Circle Collider here. 165 00:12:10,320 --> 00:12:17,940 ‫And before we test that we of course need to make that is trigger and let's see if our fruit is a trigger 166 00:12:17,940 --> 00:12:18,690 ‫as well. 167 00:12:20,960 --> 00:12:26,480 ‫So let's go to our orange or orange should be a trigger as well. 168 00:12:26,480 --> 00:12:28,280 ‫So let's go ahead and try that again. 169 00:12:30,500 --> 00:12:36,440 ‫Now, if we slice through our fruits, we can see that they get cut. 170 00:12:36,440 --> 00:12:39,890 ‫But as you might also see, it's not perfect yet. 171 00:12:39,890 --> 00:12:43,990 ‫So we will have to play around with the values of our radius here. 172 00:12:44,000 --> 00:12:53,300 ‫So let's go with a radius of let's go into the scene and see how big our radius is. 173 00:12:53,300 --> 00:12:55,070 ‫So let's go with 0.5. 174 00:12:55,070 --> 00:12:59,870 ‫Maybe that would be the radius if we compare it to an orange. 175 00:12:59,870 --> 00:13:01,670 ‫So let's drag an orange in here. 176 00:13:01,730 --> 00:13:03,020 ‫That would be too big. 177 00:13:03,020 --> 00:13:11,150 ‫You might guess so let's go with 0.3 maybe or oh point. 178 00:13:11,150 --> 00:13:13,880 ‫Well, actually, as soon as we hit the orange, we already cut it. 179 00:13:13,880 --> 00:13:14,330 ‫Right. 180 00:13:14,330 --> 00:13:16,550 ‫So all point three should be the right value. 181 00:13:16,550 --> 00:13:19,910 ‫So now blade is exactly the same size as the orange. 182 00:13:19,910 --> 00:13:22,340 ‫So as soon as we hit it, we should be good. 183 00:13:22,340 --> 00:13:23,690 ‫So let's try that. 184 00:13:24,620 --> 00:13:27,320 ‫And now let's cut some fruits. 185 00:13:28,070 --> 00:13:32,060 ‫Well, and as you see, we can slice our fruits. 186 00:13:32,090 --> 00:13:32,630 ‫Great. 187 00:13:32,930 --> 00:13:38,930 ‫All right, so see you in the next video where we are going to add the green to our game.