1 00:00:05,250 --> 00:00:06,180 ‫Welcome back. 2 00:00:06,210 --> 00:00:12,300 ‫In this video, we are going to add functionality to our bowl so that it finally can move from left 3 00:00:12,300 --> 00:00:17,940 ‫to right and bounce off the racquets, which is which is, of course, our intended functionality. 4 00:00:18,000 --> 00:00:21,210 ‫And then we will, of course, need to move our racquets. 5 00:00:21,210 --> 00:00:24,690 ‫That's what we're going to do later on in, in one of the further videos. 6 00:00:24,690 --> 00:00:26,730 ‫And then we are almost done with the game. 7 00:00:26,730 --> 00:00:37,290 ‫So let's start off by creating a new script for our ball, which I'm going to call ball movement. 8 00:00:38,370 --> 00:00:46,170 ‫And as you can see, create an ad, we create a new script, and we can open that up with Visual Studio. 9 00:00:46,950 --> 00:00:51,480 ‫All right, in Visual Studio, we have the start and the update method. 10 00:00:51,480 --> 00:00:54,180 ‫We won't need the update method, so let's get rid of that. 11 00:00:54,360 --> 00:01:01,260 ‫What we do need, however, is a method which indicates how fast the ball can move, and that should 12 00:01:01,260 --> 00:01:04,290 ‫be a public value so that we can change it within unity. 13 00:01:04,410 --> 00:01:15,630 ‫So public float movement speed speeds usually should be floats and unity because what you want to use 14 00:01:15,630 --> 00:01:20,070 ‫your speed for is most of the time physics and physics do work with float. 15 00:01:20,070 --> 00:01:27,600 ‫Then what we will need is a value which tells us how much faster the ball can go per hit. 16 00:01:27,600 --> 00:01:33,060 ‫So for each hit that it hits with with the racket, it should go a little bit faster so that the game 17 00:01:33,060 --> 00:01:35,310 ‫gets more difficult progressively. 18 00:01:35,520 --> 00:01:45,630 ‫And that's what we achieve by creating a another float variable variable, which is called extra speed 19 00:01:46,770 --> 00:01:47,820 ‫per hit. 20 00:01:47,820 --> 00:01:50,580 ‫So for every hit it should go up a little bit. 21 00:01:51,060 --> 00:01:57,120 ‫Then we need a maximum speed so that the ball doesn't go super fast at one point and is impossible to 22 00:01:57,120 --> 00:01:57,630 ‫catch. 23 00:01:57,780 --> 00:02:01,710 ‫Of course you could say, okay, I don't want that, but we want to set a cap here. 24 00:02:01,800 --> 00:02:10,290 ‫So we create a cap which is our max extra speed so that there is a maximum speed in general and we will 25 00:02:10,290 --> 00:02:13,680 ‫need a help of variable, which I call hit counter. 26 00:02:14,790 --> 00:02:19,950 ‫And that will count how many times the racket has been hit or any racket has been hit. 27 00:02:20,490 --> 00:02:23,430 ‫So now what we want to do is move our ball. 28 00:02:23,610 --> 00:02:29,030 ‫So let's create a method to move our ball public void, move ball. 29 00:02:29,160 --> 00:02:35,760 ‫So how I'm going to call it and it needs a vector to direction, so I'm just going to call that dear 30 00:02:35,760 --> 00:02:38,030 ‫for direction the air. 31 00:02:38,820 --> 00:02:42,390 ‫Now that direction should be a normalized direction. 32 00:02:42,390 --> 00:02:47,520 ‫So this move ball method can be called from outside, it can be called within another method. 33 00:02:47,520 --> 00:02:53,280 ‫For example, it can be called from a different class or from a different script because it's public. 34 00:02:53,280 --> 00:02:59,910 ‫And what we want to do is, first of all, normalize the direction that was given to us because it could 35 00:02:59,910 --> 00:03:03,150 ‫become a very weird value, but we want it to be normalized. 36 00:03:03,150 --> 00:03:10,260 ‫So what we do is we say direction is the old direction, but as a normalized value. 37 00:03:10,260 --> 00:03:13,980 ‫So it returns this vector with a magnitude of one. 38 00:03:14,670 --> 00:03:16,170 ‫So that's what we do here. 39 00:03:16,560 --> 00:03:24,210 ‫Now we can go ahead and create a speed so the speed of our ball should be a Now remember what it was. 40 00:03:24,210 --> 00:03:33,210 ‫First of all, we had the movement speed, so this dot movement speed and then we need to add the extra 41 00:03:33,210 --> 00:03:36,870 ‫speed per hit and therefore we need to know how many hits we had. 42 00:03:36,870 --> 00:03:40,680 ‫So that means we go ahead and say this dot hit counter. 43 00:03:40,680 --> 00:03:42,570 ‫So how many ever hits we had? 44 00:03:42,570 --> 00:03:46,470 ‫Multiply it with this dot extra speed per hit. 45 00:03:48,210 --> 00:03:52,380 ‫This way we increase the movement of the ball continually. 46 00:03:52,500 --> 00:04:00,450 ‫Now, in order to apply that speed to our ball, what we need to add is force to our ball and in that 47 00:04:00,450 --> 00:04:06,300 ‫case, force to the rigid body, because the rigid body is the physics aspect of our ball. 48 00:04:06,300 --> 00:04:08,640 ‫So that means we need to get a rigid body. 49 00:04:08,640 --> 00:04:20,490 ‫2D, which I call rigid body 2D is equal to this dot game object dot get component and the name of the 50 00:04:20,490 --> 00:04:23,790 ‫component should be rigid body to DX. 51 00:04:26,250 --> 00:04:30,840 ‫So we take the rigid body 2D component of this game object. 52 00:04:30,840 --> 00:04:37,770 ‫So of the game object which carries the ball movement script, which will only be our ball for now if 53 00:04:37,770 --> 00:04:42,870 ‫we want to add multiple balls, we could do that and all of them would have to have a rigid body to 54 00:04:42,870 --> 00:04:45,570 ‫DX component assigned to them. 55 00:04:45,750 --> 00:04:50,490 ‫So now that we know the rigid body 2D, we can go ahead and influence that. 56 00:04:50,490 --> 00:04:57,570 ‫So we change its velocity, its direction multiplied with speed. 57 00:04:58,110 --> 00:05:04,530 ‫So that means whatever direction was given to our move ball method will be multiplied with the. 58 00:05:05,030 --> 00:05:11,120 ‫Which is the movement speed plus the extra speed per hit multiplied with the amounts of hits. 59 00:05:11,240 --> 00:05:15,470 ‫In order for our ball to move now, we need to call move ball. 60 00:05:15,500 --> 00:05:18,410 ‫The best way to do that is by calling it within a CO routine. 61 00:05:18,410 --> 00:05:24,050 ‫So to start a code routine and that means that we don't use the update method, which would be very 62 00:05:24,060 --> 00:05:29,660 ‫inefficient because we would constantly call the method and change the speed real quick and so on. 63 00:05:29,660 --> 00:05:35,060 ‫And the best way to do it is really to create a i enumerator method. 64 00:05:35,060 --> 00:05:42,080 ‫So a method which is, which returns an enumerator so that we can use it for a call routine. 65 00:05:42,200 --> 00:05:43,160 ‫What a call routine. 66 00:05:43,160 --> 00:05:47,660 ‫Does it forces everything else to wait for it before new actions happen. 67 00:05:47,840 --> 00:05:52,280 ‫This way you can control that one action will wait for another instead of running. 68 00:05:52,280 --> 00:05:58,790 ‫In parallel, we will find more interesting ways to start code routine in further tutorials, but now 69 00:05:58,970 --> 00:06:00,950 ‫let's just use that to move our ball. 70 00:06:01,520 --> 00:06:09,530 ‫So what we need first is a new method which is public, which returns an AI enumerator, which is, 71 00:06:09,560 --> 00:06:13,940 ‫as you can see, supports a simple iteration of our non generic collection. 72 00:06:13,940 --> 00:06:15,140 ‫So that's what we want. 73 00:06:15,470 --> 00:06:17,480 ‫We call that one start ball. 74 00:06:17,480 --> 00:06:23,330 ‫So that means this method will be called whenever we start the ball's movement and we need a boolean 75 00:06:23,330 --> 00:06:29,420 ‫here which is called is starting player one equals true. 76 00:06:29,420 --> 00:06:36,710 ‫So that means we need a variable which is our starting player and if no value is given it will be simply 77 00:06:36,710 --> 00:06:37,310 ‫true. 78 00:06:37,340 --> 00:06:40,040 ‫That means that our player one will be the starting player. 79 00:06:40,040 --> 00:06:42,770 ‫So that means the ball will go from left to right. 80 00:06:43,800 --> 00:06:44,490 ‫All right. 81 00:06:45,150 --> 00:06:47,760 ‫And what that one should be called start ball. 82 00:06:48,090 --> 00:06:49,830 ‫Now, we still get the red underline. 83 00:06:49,830 --> 00:06:51,710 ‫That means that we still have an error here. 84 00:06:51,720 --> 00:06:55,080 ‫That is because we don't return something. 85 00:06:55,770 --> 00:06:59,490 ‫Not all cold puffs return a value, so let's return some values. 86 00:06:59,670 --> 00:07:03,540 ‫What we need, therefore, is, first of all, we need to reset our hit counter. 87 00:07:03,810 --> 00:07:13,650 ‫So we set that to zero here and then we yield return a new weight 4 seconds. 88 00:07:13,650 --> 00:07:15,780 ‫So that means we make our game. 89 00:07:15,780 --> 00:07:17,250 ‫Wait for 2 seconds. 90 00:07:22,510 --> 00:07:28,060 ‫So that means whenever we start the game or we whenever start ball is called, we wait for 2 seconds 91 00:07:28,060 --> 00:07:31,720 ‫after we set the hit counter to zero and then we start the movement. 92 00:07:31,720 --> 00:07:38,500 ‫So that means we check it's the first player starting or is player to starting. 93 00:07:38,500 --> 00:07:42,190 ‫So this player is starting player one. 94 00:07:42,760 --> 00:07:44,800 ‫If that's the case, go ahead. 95 00:07:44,800 --> 00:07:46,780 ‫This dot move ball. 96 00:07:46,780 --> 00:07:58,390 ‫So we call this method here and we give it a vector of new vector two with the value minus one and zero. 97 00:07:58,810 --> 00:08:07,360 ‫That means the ball will move to the left and otherwise the ball should move to the right. 98 00:08:08,140 --> 00:08:15,790 ‫So this dot move ball new vector two now it should move to the right. 99 00:08:15,790 --> 00:08:17,110 ‫So one and zero. 100 00:08:17,410 --> 00:08:21,610 ‫So one is the x value and zero is the Y value as you can see, float y. 101 00:08:21,700 --> 00:08:25,000 ‫All right, now that we have that, we only need to start a CO routine. 102 00:08:25,000 --> 00:08:32,440 ‫So start CO routine and in here we call our this dot start ball. 103 00:08:34,660 --> 00:08:35,800 ‫All right, now let's save that. 104 00:08:35,800 --> 00:08:39,430 ‫We will need to add some further functionality to the game later on. 105 00:08:39,430 --> 00:08:42,340 ‫But for now, let's test that within unity. 106 00:08:42,340 --> 00:08:45,760 ‫So what I need now is to change the mass of our ball. 107 00:08:45,760 --> 00:08:47,050 ‫It should be super lightweight. 108 00:08:47,050 --> 00:08:50,080 ‫So I'm just going to use 0.01 109 00:08:52,240 --> 00:08:55,780 ‫and for the gravity scale, I'm just going to go for zero. 110 00:08:55,780 --> 00:08:58,060 ‫We don't need any gravity for our ball at all. 111 00:08:59,410 --> 00:09:05,410 ‫Now, as you can see here, the movement speed is at zero, extra speed per hit is at zero and our max 112 00:09:05,410 --> 00:09:07,360 ‫extra speed are zero as well. 113 00:09:07,360 --> 00:09:08,740 ‫And that's not something that we want. 114 00:09:08,770 --> 00:09:14,590 ‫Of course, we want our ball to move and a good starting value is 400. 115 00:09:14,920 --> 00:09:19,720 ‫A good extra speed per hit is 50 and the maximum speed should be 1000. 116 00:09:19,720 --> 00:09:23,560 ‫So these are just values that I have come up with after testing. 117 00:09:23,740 --> 00:09:27,910 ‫And let's see what happens after 2 seconds. 118 00:09:27,910 --> 00:09:34,900 ‫Our ball starts moving to the left because it's player one's turn and then it goes back and forth. 119 00:09:34,900 --> 00:09:39,550 ‫As you can see, the speed is very equal, so it doesn't really change. 120 00:09:39,550 --> 00:09:45,550 ‫It's always the same speed even though we have added this extra speed per hit value. 121 00:09:45,550 --> 00:09:53,200 ‫So let's go ahead and increase the extra speed per hit with its hit counter. 122 00:09:53,200 --> 00:09:59,560 ‫So we need to add a method which is increasing our hit counter by one and I'm going to call that one 123 00:10:02,380 --> 00:10:08,470 ‫public void increase hit counter. 124 00:10:10,390 --> 00:10:13,300 ‫So what that will do is it will increase the hit counter by one. 125 00:10:13,300 --> 00:10:24,490 ‫So it says if this dot hit counter multiplied by this dot, extra speed is smaller than this dot max 126 00:10:24,490 --> 00:10:25,630 ‫extra speed. 127 00:10:26,560 --> 00:10:34,570 ‫Then please go ahead and increase the hit counter because otherwise we would have a speed of well at 128 00:10:34,570 --> 00:10:36,550 ‫one point and unlimited speed. 129 00:10:36,550 --> 00:10:40,660 ‫So it will go up, up, up and up and further up. 130 00:10:41,500 --> 00:10:41,860 ‫All right. 131 00:10:41,860 --> 00:10:48,460 ‫That that method is not called at all now, and it will not be called within our ball movement script. 132 00:10:48,460 --> 00:10:52,600 ‫We will call it, however, in the script for our records. 133 00:10:52,600 --> 00:10:55,570 ‫So we're going to do that in one of the next videos. 134 00:10:55,570 --> 00:11:01,420 ‫But so far you have seen how to add movement to your ball, and we will go on with that in the next 135 00:11:01,420 --> 00:11:02,080 ‫videos. 136 00:11:02,080 --> 00:11:03,940 ‫So see you in the next video. 137 00:11:03,940 --> 00:11:10,240 ‫Before we do that, of course we need to clean up, so let's drag that ball movement into scripts and 138 00:11:10,240 --> 00:11:13,930 ‫let's save our scene and see you in the next video.