Welcome back.
In this video, we are going to take care of the bouncing off of our ball in the correct way so that
it is more interactive and makes more fun in general.
Here you can see how the ball can come from the left hand side, for example, onto onto the racquet.
And if it bounces off the bottom of the racket, it should go towards the bottom.
So the y value should show down.
And if it goes towards the top of the racket, it should go up.
So the ball should bounce towards the top as well.
So it should have a positive Y value in this case.
In order to achieve that, we will need a script and that script is something that we will need to add
to our ball.
So I'm going to add a new script to our ball.
So here, create new script and I'm going to call it Collision Controller because that's going to control
our collision, which means it will check how it collides and where it collides and so on.
It should not only collide like that from the racquet, but it should also collide like that from the
top and the bottom wall.
All right.
So what do we need for that?
We need to have the ball movement controller so that we can access its methods.
So let's create a public ball movement, which I call ball movement.
So now we have an object of our ball movement script.
Then we will need a method which takes care of the balance.
So I'm going to call that bounce from break from racket.
And here we need a collision to DX object, which I call C.
Now in order to know if the record should go towards the top or to the bottom.
We need to have the ball position.
We need to have the record position, we need to have the record height and then we can calculate whether
it should go up or down.
So let's create a vector three, vector three for our ball position.
Then I'm going to use this dot transform dot position.
So whatever the position of this game object is, whichever carries the collision controller in this
case our ball from that the position should be the ball position.
So that's what we have here.
Then we need to make three, which is the racquet position and it should be the game object that we
collide with.
So for example, the racquet should be the game object.
So that means see the game object.
So the means whatever game object we collide with from that, we want to have the position and now we
need a height, which is the height of our racquet float, racquet height which is C dot collider dot
bounce, dot size dot y.
So we take the size of our racquet that we collide with in order to know at which point we hit it so
that we hit it at the top or did we hit it at the bottom area of it?
Next, we need to know if we bounce off the record of player one on the left hand side or Player two
on the right hand side.
Based on that, we change the direction of our ball.
So if it bounces off the of the player one record, the direction goes from minus X to plus x.
And on the other hand, from player two, it goes from plus X to minus x and we can achieve that by
saving another value, float X or creating another variable.
And if the game object that name is equal to racket player one,
then set the x value to one.
So go back towards the right.
So fly it to the right please, and else x equals minus one.
Now we only need to calculate the y value because if we have the x and the y value, we can create a
vector too, which is the new direction of our ball.
So that means y is equal to ball position.
Why?
Minus record position dot y divided by the record height.
Now that we have that, we can go ahead and call our ball movement game object and call its method increase,
counter increase, hit counter to be precise.
And if you remember, that's the method that increases the hit count, which again increases the speed
of our ball.
So here we can call it, and then we can go ahead and call the move ball method, which will now need
a vector to which is the direction of the ball and that should be X and Y.
So towards either towards the right or towards the left.
And then here we decide how high it should go.
Should it go towards the bottom or should it go towards the top?
So that's our bounce from rocket method.
Now we need to go ahead and run the right code routine because so far we had a code routine which was
our ball just moving left and right.
And now we need to change the code routine a little bit.
So that means we need a void on collision.
Enter to DH.
So whenever there is a two RD collision, we want to go ahead and run certain code.
So that means whenever our ball collides with something, we will get a collision.
Now we check if the game object that we collide with.
So collision dot game object dot name is equal to record player one.
Or equal to record player to.
So I'm just gonna copy that
record player, too.
So if it.
If we hit the record, we don't want to do that.
If we hit, for example, the bottom wall or a top wall or something like that.
We only want to do that if we hit one of the two records.
So we need a double X double comparison sine double equals sine, of course.
Otherwise, we run into errors here because and then what we do is this dot bounce from record.
With our collision.
So if we hit the ball, we do that with the collision.
LS If we hit the wall left.
See that game object or collision that game object thought name is equal to wall left.
Then here we want to increase the score.
So let's say debug log, we will add that functionality later on.
So increasing the score in general, the score system, we say collision with wall left
and then we do the same thing with Wall Right.
So that means if the right wall is hit, that means that our player one gets a score.
If the left wall was hit.
Player two gets a score or gets a point.
All right.
So now that we have that, let's save the script.
Now, before we test that, we need to provide a script here, the ball movement script for our collision
controller.
And now that we've done that, we can go ahead and test our game.
So let's run the game after 2 seconds.
Our ball should move and as you can see, it changes its direction based on where it was hit.
So now it should go down perfect.
It should go up again and it goes up again and again.
So as you can see, we have the functionality we wanted.
And of course, as you can see as well, our ball goes faster and faster.
And at this point it's impossible for me to to hit it.
And also our debug collision with ball left and collision with ball right are called.
Maybe we should change the collision with Wall right here actually.
So we'll right now the debug log would show the right thing here in here.
We will need to increase the score of the player later on and we will need to position the ball back
at the center.
All right.
So that's what we're going to do in the next video.
So see you there.