1 00:00:04,020 --> 00:00:04,800 ‫Welcome back. 2 00:00:04,830 --> 00:00:10,260 ‫In this video, you will learn how to use Rakes, which is a powerful tool which you can use on in many 3 00:00:10,260 --> 00:00:11,040 ‫different locations. 4 00:00:11,040 --> 00:00:16,710 ‫You can, for example, check if there is something in front of you behind you, on top of you, or 5 00:00:16,710 --> 00:00:20,160 ‫at the point where your mouse is and all of those kind of things. 6 00:00:20,160 --> 00:00:22,770 ‫And we will use that in two different games. 7 00:00:22,770 --> 00:00:26,310 ‫So let's go ahead and do it now so that you know how to use it later on. 8 00:00:26,400 --> 00:00:28,950 ‫What we need for that is our cube. 9 00:00:28,950 --> 00:00:36,840 ‫So let's bring our cube back into the game and at the same time, let's create a plane. 10 00:00:37,560 --> 00:00:41,190 ‫So 3D object and plane. 11 00:00:41,550 --> 00:00:45,180 ‫So now things look a bit different here. 12 00:00:45,210 --> 00:00:49,590 ‫As you can see, our angle is very weird. 13 00:00:49,800 --> 00:00:52,070 ‫So let's have a look at this. 14 00:00:52,650 --> 00:00:56,370 ‫Plane is within the cube, so let's change the rotation of the plane. 15 00:00:57,000 --> 00:00:57,360 ‫All right. 16 00:00:57,360 --> 00:00:58,500 ‫Now we're good to go. 17 00:00:59,650 --> 00:00:59,850 ‫Okay. 18 00:01:00,060 --> 00:01:04,290 ‫So our plane should be lower than our cube. 19 00:01:04,290 --> 00:01:07,260 ‫And at the same time, I'm going to give it a material. 20 00:01:07,260 --> 00:01:14,490 ‫So let's create a material real quick so that we can see it better under ground. 21 00:01:15,870 --> 00:01:18,210 ‫And let's change that to red. 22 00:01:18,630 --> 00:01:20,730 ‫So let's make the underground red. 23 00:01:20,730 --> 00:01:22,470 ‫So now we can see it in our game. 24 00:01:22,470 --> 00:01:28,680 ‫So the idea is to check if our cube currently has something below it. 25 00:01:28,680 --> 00:01:32,640 ‫So is there if there is anything that it collides with underneath it. 26 00:01:33,000 --> 00:01:37,500 ‫And in order to do that, we can give that cube a new script. 27 00:01:37,500 --> 00:01:44,220 ‫So I'm going to add a new script to it and check for objects. 28 00:01:44,470 --> 00:01:46,560 ‫I'm going to call it check for objects. 29 00:01:47,760 --> 00:01:55,530 ‫And what we need here is within our update method, we will need a red cast hit. 30 00:01:56,430 --> 00:02:05,610 ‫So it's going to request towards a specific direction and we check if there is a hit and if physics. 31 00:02:07,600 --> 00:02:16,770 ‫Thought recast and here are 16 different overloads and we'll use one of them, the one with transformed 32 00:02:16,960 --> 00:02:17,590 ‫position. 33 00:02:17,590 --> 00:02:20,820 ‫So that's the origin of our request. 34 00:02:20,830 --> 00:02:23,050 ‫Then we need the direction of the recast. 35 00:02:23,050 --> 00:02:25,960 ‫So in my case I want to look down. 36 00:02:25,960 --> 00:02:28,180 ‫So vector three up. 37 00:02:28,180 --> 00:02:30,790 ‫So minus vector three up means go down. 38 00:02:31,540 --> 00:02:35,470 ‫And then we want to have the point which we hit. 39 00:02:35,470 --> 00:02:43,570 ‫So output hit and finally we want to know how far we want to go. 40 00:02:43,570 --> 00:02:47,080 ‫So for example, we only want to check 100 units. 41 00:02:47,080 --> 00:02:49,360 ‫We don't want to go any further than that. 42 00:02:49,540 --> 00:03:00,250 ‫And if we hit something, then we can, for example, print, we hit something and we could even go 43 00:03:00,250 --> 00:03:10,300 ‫as far as telling the different distance to the object at plus hit dot distance. 44 00:03:12,370 --> 00:03:20,260 ‫Or maybe even hit that transform thought position so we can even say where we hit something. 45 00:03:23,200 --> 00:03:25,880 ‫All right, so let's save that script and go back to Unity. 46 00:03:25,900 --> 00:03:28,330 ‫So now our cube has this script. 47 00:03:28,450 --> 00:03:30,280 ‫So now let's go ahead and run it. 48 00:03:30,400 --> 00:03:32,710 ‫Let's have a look at the console, what it says. 49 00:03:32,710 --> 00:03:35,530 ‫And it says, We hit something at zero zero. 50 00:03:35,620 --> 00:03:41,760 ‫And now I'm going to move it a little bit and I'm going to clear everything. 51 00:03:41,770 --> 00:03:49,030 ‫Let's see, nothing is hit and we move just towards our on the ground and we see that the method is 52 00:03:49,030 --> 00:03:51,070 ‫called and it's called in the update method. 53 00:03:51,070 --> 00:03:52,900 ‫So it's called very often. 54 00:03:52,900 --> 00:03:57,400 ‫So our print method is called like very often in every single frame. 55 00:03:57,460 --> 00:04:01,930 ‫So now we know precisely when we hit something. 56 00:04:02,140 --> 00:04:12,310 ‫What we even could do is go as far as telling if we don't hit anything so else print nothing underneath 57 00:04:13,000 --> 00:04:13,600 ‫us. 58 00:04:16,000 --> 00:04:18,340 ‫So let's save that and try it again. 59 00:04:18,830 --> 00:04:25,660 ‫And now let's move our element back and forth, nothing underneath us. 60 00:04:25,660 --> 00:04:29,560 ‫And we hit something and they move back and forth. 61 00:04:29,830 --> 00:04:30,400 ‫All right. 62 00:04:30,400 --> 00:04:34,780 ‫So I'm still getting this error because I have something in the build that I'm not using anymore. 63 00:04:34,810 --> 00:04:36,910 ‫So that shouldn't appear for you. 64 00:04:37,650 --> 00:04:43,240 ‫Okay, so now you know how to use Red Cast Hit to check if something is underneath you or not. 65 00:04:43,810 --> 00:04:49,570 ‫Now let's have a look at how we can do the same thing for moving over with our mouse and seeing if we 66 00:04:49,600 --> 00:04:51,280 ‫hit something with our mouse. 67 00:04:51,700 --> 00:04:53,650 ‫So let's stop the game. 68 00:04:53,650 --> 00:04:55,420 ‫Let's go back to Visual Studio. 69 00:04:55,420 --> 00:04:59,530 ‫And now, instead of using recast hit, we will need to use Ray. 70 00:05:00,010 --> 00:05:07,870 ‫So let's comment that out and we'll use Ray now. 71 00:05:08,890 --> 00:05:17,560 ‫So Ray Ray is equal to camera dot main, that screen point to Ray. 72 00:05:17,590 --> 00:05:23,320 ‫And in here we use input before we always use get mouse down and so on. 73 00:05:23,320 --> 00:05:30,340 ‫But this time we use the mouse position, which is a vector three, which checks where is our mouse 74 00:05:30,340 --> 00:05:35,500 ‫and then calculates at which point in our game is it at. 75 00:05:36,700 --> 00:05:51,370 ‫And now we can check if physics dot recast with our ray and for example, the distance of 100 if there 76 00:05:51,370 --> 00:05:53,470 ‫is something and if there is something. 77 00:05:53,470 --> 00:05:54,220 ‫Print. 78 00:05:57,270 --> 00:05:58,920 ‫Hit and hit. 79 00:05:58,920 --> 00:06:00,240 ‫Uh, maybe something. 80 00:06:00,780 --> 00:06:01,770 ‫Hit something. 81 00:06:02,070 --> 00:06:03,150 ‫So let's have a look. 82 00:06:03,150 --> 00:06:04,260 ‫What happens now? 83 00:06:04,410 --> 00:06:11,670 ‫Let's go back into unity, and let's play around with our mouths and see if we hit something. 84 00:06:13,170 --> 00:06:15,690 ‫And we hit something, and I hit the bottom. 85 00:06:15,690 --> 00:06:21,600 ‫And so now I can see every time that I hit something or update method goes up for every single frame 86 00:06:21,600 --> 00:06:22,770 ‫that I hit something. 87 00:06:24,630 --> 00:06:31,260 ‫Now, if I want to know what I've hit, I can use rate cast hit and I'm just going to call it hit. 88 00:06:31,800 --> 00:06:37,770 ‫And in here I will need to use a different overload, the out hit overload. 89 00:06:38,340 --> 00:06:39,670 ‫So now I'll know what I will. 90 00:06:39,690 --> 00:06:49,800 ‫What I have hit and hit something I replace that with hit dot collider, which means the the collider 91 00:06:49,800 --> 00:06:50,700 ‫that I have hit. 92 00:06:51,690 --> 00:06:59,100 ‫And from that collider, I want to know the game object itself and maybe even the name of the game object. 93 00:06:59,910 --> 00:07:01,170 ‫So that will give me the name. 94 00:07:01,170 --> 00:07:02,580 ‫So let's say I hit. 95 00:07:04,090 --> 00:07:06,550 ‫The Cube, then it should say, Hit Cube. 96 00:07:06,580 --> 00:07:11,080 ‫If I hit our underground, our plane, then it should say plane. 97 00:07:11,140 --> 00:07:12,550 ‫So let's check that out. 98 00:07:13,720 --> 00:07:17,290 ‫Let's go somewhere with our mouse and let's walk over it. 99 00:07:17,320 --> 00:07:19,240 ‫Hit cube and hit plane. 100 00:07:19,750 --> 00:07:23,620 ‫As you can see now, we even know which object we have hit. 101 00:07:23,620 --> 00:07:30,340 ‫And that one will allow us to, for example, build buildings because we know where we currently are 102 00:07:30,340 --> 00:07:33,790 ‫with the mouse and we know where we are positioning a building. 103 00:07:33,790 --> 00:07:36,850 ‫That's something that we will do in the Fun World Clone chapter. 104 00:07:36,850 --> 00:07:42,610 ‫So you might want to come back to this video if you want to build the Farmville clone by yourself. 105 00:07:42,910 --> 00:07:43,300 ‫All right. 106 00:07:43,300 --> 00:07:47,950 ‫So see you in the next video where we will finally start off with the game itself.