1 00:00:04,080 --> 00:00:05,010 ‫Welcome back. 2 00:00:05,040 --> 00:00:11,610 ‫In this video, we are going to take care of the bouncing off of our ball in the correct way so that 3 00:00:11,610 --> 00:00:14,500 ‫it is more interactive and makes more fun in general. 4 00:00:14,520 --> 00:00:19,680 ‫Here you can see how the ball can come from the left hand side, for example, onto onto the racquet. 5 00:00:19,680 --> 00:00:25,470 ‫And if it bounces off the bottom of the racket, it should go towards the bottom. 6 00:00:25,470 --> 00:00:27,990 ‫So the y value should show down. 7 00:00:27,990 --> 00:00:32,160 ‫And if it goes towards the top of the racket, it should go up. 8 00:00:32,160 --> 00:00:34,920 ‫So the ball should bounce towards the top as well. 9 00:00:34,920 --> 00:00:37,770 ‫So it should have a positive Y value in this case. 10 00:00:39,420 --> 00:00:45,000 ‫In order to achieve that, we will need a script and that script is something that we will need to add 11 00:00:45,000 --> 00:00:45,960 ‫to our ball. 12 00:00:46,230 --> 00:00:49,770 ‫So I'm going to add a new script to our ball. 13 00:00:50,010 --> 00:01:00,030 ‫So here, create new script and I'm going to call it Collision Controller because that's going to control 14 00:01:00,030 --> 00:01:04,800 ‫our collision, which means it will check how it collides and where it collides and so on. 15 00:01:04,800 --> 00:01:08,880 ‫It should not only collide like that from the racquet, but it should also collide like that from the 16 00:01:08,880 --> 00:01:10,410 ‫top and the bottom wall. 17 00:01:11,010 --> 00:01:11,370 ‫All right. 18 00:01:11,370 --> 00:01:12,660 ‫So what do we need for that? 19 00:01:12,990 --> 00:01:17,790 ‫We need to have the ball movement controller so that we can access its methods. 20 00:01:17,790 --> 00:01:23,190 ‫So let's create a public ball movement, which I call ball movement. 21 00:01:24,150 --> 00:01:26,880 ‫So now we have an object of our ball movement script. 22 00:01:26,880 --> 00:01:29,880 ‫Then we will need a method which takes care of the balance. 23 00:01:29,880 --> 00:01:35,580 ‫So I'm going to call that bounce from break from racket. 24 00:01:37,650 --> 00:01:43,800 ‫And here we need a collision to DX object, which I call C. 25 00:01:45,530 --> 00:01:49,880 ‫Now in order to know if the record should go towards the top or to the bottom. 26 00:01:49,880 --> 00:01:52,070 ‫We need to have the ball position. 27 00:01:52,070 --> 00:01:57,590 ‫We need to have the record position, we need to have the record height and then we can calculate whether 28 00:01:57,590 --> 00:01:59,240 ‫it should go up or down. 29 00:01:59,240 --> 00:02:06,080 ‫So let's create a vector three, vector three for our ball position. 30 00:02:07,260 --> 00:02:12,600 ‫Then I'm going to use this dot transform dot position. 31 00:02:12,600 --> 00:02:18,420 ‫So whatever the position of this game object is, whichever carries the collision controller in this 32 00:02:18,420 --> 00:02:22,860 ‫case our ball from that the position should be the ball position. 33 00:02:22,860 --> 00:02:24,630 ‫So that's what we have here. 34 00:02:24,630 --> 00:02:33,030 ‫Then we need to make three, which is the racquet position and it should be the game object that we 35 00:02:33,030 --> 00:02:33,600 ‫collide with. 36 00:02:33,600 --> 00:02:36,540 ‫So for example, the racquet should be the game object. 37 00:02:36,540 --> 00:02:38,700 ‫So that means see the game object. 38 00:02:38,700 --> 00:02:47,490 ‫So the means whatever game object we collide with from that, we want to have the position and now we 39 00:02:47,490 --> 00:02:56,490 ‫need a height, which is the height of our racquet float, racquet height which is C dot collider dot 40 00:02:56,490 --> 00:03:01,200 ‫bounce, dot size dot y. 41 00:03:01,200 --> 00:03:08,880 ‫So we take the size of our racquet that we collide with in order to know at which point we hit it so 42 00:03:08,880 --> 00:03:12,630 ‫that we hit it at the top or did we hit it at the bottom area of it? 43 00:03:12,660 --> 00:03:18,570 ‫Next, we need to know if we bounce off the record of player one on the left hand side or Player two 44 00:03:18,570 --> 00:03:19,650 ‫on the right hand side. 45 00:03:19,770 --> 00:03:23,280 ‫Based on that, we change the direction of our ball. 46 00:03:23,280 --> 00:03:31,410 ‫So if it bounces off the of the player one record, the direction goes from minus X to plus x. 47 00:03:32,640 --> 00:03:39,990 ‫And on the other hand, from player two, it goes from plus X to minus x and we can achieve that by 48 00:03:39,990 --> 00:03:44,610 ‫saving another value, float X or creating another variable. 49 00:03:44,610 --> 00:03:53,760 ‫And if the game object that name is equal to racket player one, 50 00:03:56,580 --> 00:03:59,580 ‫then set the x value to one. 51 00:04:00,180 --> 00:04:02,160 ‫So go back towards the right. 52 00:04:02,160 --> 00:04:08,880 ‫So fly it to the right please, and else x equals minus one. 53 00:04:10,740 --> 00:04:16,950 ‫Now we only need to calculate the y value because if we have the x and the y value, we can create a 54 00:04:16,950 --> 00:04:20,010 ‫vector too, which is the new direction of our ball. 55 00:04:20,580 --> 00:04:25,470 ‫So that means y is equal to ball position. 56 00:04:28,110 --> 00:04:28,860 ‫Why? 57 00:04:32,380 --> 00:04:38,620 ‫Minus record position dot y divided by the record height. 58 00:04:41,230 --> 00:04:48,490 ‫Now that we have that, we can go ahead and call our ball movement game object and call its method increase, 59 00:04:48,490 --> 00:04:52,210 ‫counter increase, hit counter to be precise. 60 00:04:52,210 --> 00:04:58,060 ‫And if you remember, that's the method that increases the hit count, which again increases the speed 61 00:04:58,060 --> 00:04:59,260 ‫of our ball. 62 00:04:59,950 --> 00:05:06,820 ‫So here we can call it, and then we can go ahead and call the move ball method, which will now need 63 00:05:06,820 --> 00:05:12,160 ‫a vector to which is the direction of the ball and that should be X and Y. 64 00:05:12,190 --> 00:05:17,350 ‫So towards either towards the right or towards the left. 65 00:05:17,350 --> 00:05:20,290 ‫And then here we decide how high it should go. 66 00:05:20,290 --> 00:05:23,590 ‫Should it go towards the bottom or should it go towards the top? 67 00:05:25,330 --> 00:05:27,920 ‫So that's our bounce from rocket method. 68 00:05:27,940 --> 00:05:33,550 ‫Now we need to go ahead and run the right code routine because so far we had a code routine which was 69 00:05:33,550 --> 00:05:35,980 ‫our ball just moving left and right. 70 00:05:35,980 --> 00:05:39,130 ‫And now we need to change the code routine a little bit. 71 00:05:39,130 --> 00:05:43,450 ‫So that means we need a void on collision. 72 00:05:45,820 --> 00:05:46,900 ‫Enter to DH. 73 00:05:46,930 --> 00:05:52,540 ‫So whenever there is a two RD collision, we want to go ahead and run certain code. 74 00:05:52,540 --> 00:05:57,790 ‫So that means whenever our ball collides with something, we will get a collision. 75 00:05:58,870 --> 00:06:03,040 ‫Now we check if the game object that we collide with. 76 00:06:03,040 --> 00:06:11,020 ‫So collision dot game object dot name is equal to record player one. 77 00:06:15,730 --> 00:06:18,730 ‫Or equal to record player to. 78 00:06:23,960 --> 00:06:25,550 ‫So I'm just gonna copy that 79 00:06:29,510 --> 00:06:30,480 ‫record player, too. 80 00:06:30,500 --> 00:06:31,220 ‫So if it. 81 00:06:31,220 --> 00:06:34,280 ‫If we hit the record, we don't want to do that. 82 00:06:34,280 --> 00:06:38,340 ‫If we hit, for example, the bottom wall or a top wall or something like that. 83 00:06:38,360 --> 00:06:42,050 ‫We only want to do that if we hit one of the two records. 84 00:06:42,980 --> 00:06:48,350 ‫So we need a double X double comparison sine double equals sine, of course. 85 00:06:48,800 --> 00:06:57,020 ‫Otherwise, we run into errors here because and then what we do is this dot bounce from record. 86 00:06:59,470 --> 00:07:01,310 ‫With our collision. 87 00:07:02,170 --> 00:07:05,240 ‫So if we hit the ball, we do that with the collision. 88 00:07:05,260 --> 00:07:07,390 ‫LS If we hit the wall left. 89 00:07:10,560 --> 00:07:17,730 ‫See that game object or collision that game object thought name is equal to wall left. 90 00:07:23,280 --> 00:07:25,320 ‫Then here we want to increase the score. 91 00:07:25,320 --> 00:07:30,180 ‫So let's say debug log, we will add that functionality later on. 92 00:07:30,180 --> 00:07:39,720 ‫So increasing the score in general, the score system, we say collision with wall left 93 00:07:42,600 --> 00:07:46,230 ‫and then we do the same thing with Wall Right. 94 00:07:49,180 --> 00:07:53,310 ‫So that means if the right wall is hit, that means that our player one gets a score. 95 00:07:53,320 --> 00:07:54,770 ‫If the left wall was hit. 96 00:07:54,790 --> 00:07:57,340 ‫Player two gets a score or gets a point. 97 00:07:58,720 --> 00:07:59,290 ‫All right. 98 00:07:59,290 --> 00:08:01,840 ‫So now that we have that, let's save the script. 99 00:08:02,440 --> 00:08:09,850 ‫Now, before we test that, we need to provide a script here, the ball movement script for our collision 100 00:08:09,850 --> 00:08:10,600 ‫controller. 101 00:08:10,990 --> 00:08:14,140 ‫And now that we've done that, we can go ahead and test our game. 102 00:08:14,290 --> 00:08:17,290 ‫So let's run the game after 2 seconds. 103 00:08:17,290 --> 00:08:23,080 ‫Our ball should move and as you can see, it changes its direction based on where it was hit. 104 00:08:23,080 --> 00:08:25,060 ‫So now it should go down perfect. 105 00:08:25,510 --> 00:08:30,490 ‫It should go up again and it goes up again and again. 106 00:08:30,490 --> 00:08:34,330 ‫So as you can see, we have the functionality we wanted. 107 00:08:34,330 --> 00:08:37,900 ‫And of course, as you can see as well, our ball goes faster and faster. 108 00:08:37,900 --> 00:08:41,370 ‫And at this point it's impossible for me to to hit it. 109 00:08:41,380 --> 00:08:47,770 ‫And also our debug collision with ball left and collision with ball right are called. 110 00:08:47,770 --> 00:08:53,530 ‫Maybe we should change the collision with Wall right here actually. 111 00:08:54,850 --> 00:09:00,730 ‫So we'll right now the debug log would show the right thing here in here. 112 00:09:00,730 --> 00:09:06,940 ‫We will need to increase the score of the player later on and we will need to position the ball back 113 00:09:06,940 --> 00:09:07,870 ‫at the center. 114 00:09:08,470 --> 00:09:08,830 ‫All right. 115 00:09:08,830 --> 00:09:11,260 ‫So that's what we're going to do in the next video. 116 00:09:11,260 --> 00:09:12,400 ‫So see you there.