‫Welcome back.
‫In this video, we are going to add functionality to our bowl so that it finally can move from left
‫to right and bounce off the racquets, which is which is, of course, our intended functionality.
‫And then we will, of course, need to move our racquets.
‫That's what we're going to do later on in, in one of the further videos.
‫And then we are almost done with the game.
‫So let's start off by creating a new script for our ball, which I'm going to call ball movement.
‫And as you can see, create an ad, we create a new script, and we can open that up with Visual Studio.
‫All right, in Visual Studio, we have the start and the update method.
‫We won't need the update method, so let's get rid of that.
‫What we do need, however, is a method which indicates how fast the ball can move, and that should
‫be a public value so that we can change it within unity.
‫So public float movement speed speeds usually should be floats and unity because what you want to use
‫your speed for is most of the time physics and physics do work with float.
‫Then what we will need is a value which tells us how much faster the ball can go per hit.
‫So for each hit that it hits with with the racket, it should go a little bit faster so that the game
‫gets more difficult progressively.
‫And that's what we achieve by creating a another float variable variable, which is called extra speed
‫per hit.
‫So for every hit it should go up a little bit.
‫Then we need a maximum speed so that the ball doesn't go super fast at one point and is impossible to
‫catch.
‫Of course you could say, okay, I don't want that, but we want to set a cap here.
‫So we create a cap which is our max extra speed so that there is a maximum speed in general and we will
‫need a help of variable, which I call hit counter.
‫And that will count how many times the racket has been hit or any racket has been hit.
‫So now what we want to do is move our ball.
‫So let's create a method to move our ball public void, move ball.
‫So how I'm going to call it and it needs a vector to direction, so I'm just going to call that dear
‫for direction the air.
‫Now that direction should be a normalized direction.
‫So this move ball method can be called from outside, it can be called within another method.
‫For example, it can be called from a different class or from a different script because it's public.
‫And what we want to do is, first of all, normalize the direction that was given to us because it could
‫become a very weird value, but we want it to be normalized.
‫So what we do is we say direction is the old direction, but as a normalized value.
‫So it returns this vector with a magnitude of one.
‫So that's what we do here.
‫Now we can go ahead and create a speed so the speed of our ball should be a Now remember what it was.
‫First of all, we had the movement speed, so this dot movement speed and then we need to add the extra
‫speed per hit and therefore we need to know how many hits we had.
‫So that means we go ahead and say this dot hit counter.
‫So how many ever hits we had?
‫Multiply it with this dot extra speed per hit.
‫This way we increase the movement of the ball continually.
‫Now, in order to apply that speed to our ball, what we need to add is force to our ball and in that
‫case, force to the rigid body, because the rigid body is the physics aspect of our ball.
‫So that means we need to get a rigid body.
‫2D, which I call rigid body 2D is equal to this dot game object dot get component and the name of the
‫component should be rigid body to DX.
‫So we take the rigid body 2D component of this game object.
‫So of the game object which carries the ball movement script, which will only be our ball for now if
‫we want to add multiple balls, we could do that and all of them would have to have a rigid body to
‫DX component assigned to them.
‫So now that we know the rigid body 2D, we can go ahead and influence that.
‫So we change its velocity, its direction multiplied with speed.
‫So that means whatever direction was given to our move ball method will be multiplied with the.
‫Which is the movement speed plus the extra speed per hit multiplied with the amounts of hits.
‫In order for our ball to move now, we need to call move ball.
‫The best way to do that is by calling it within a CO routine.
‫So to start a code routine and that means that we don't use the update method, which would be very
‫inefficient because we would constantly call the method and change the speed real quick and so on.
‫And the best way to do it is really to create a i enumerator method.
‫So a method which is, which returns an enumerator so that we can use it for a call routine.
‫What a call routine.
‫Does it forces everything else to wait for it before new actions happen.
‫This way you can control that one action will wait for another instead of running.
‫In parallel, we will find more interesting ways to start code routine in further tutorials, but now
‫let's just use that to move our ball.
‫So what we need first is a new method which is public, which returns an AI enumerator, which is,
‫as you can see, supports a simple iteration of our non generic collection.
‫So that's what we want.
‫We call that one start ball.
‫So that means this method will be called whenever we start the ball's movement and we need a boolean
‫here which is called is starting player one equals true.
‫So that means we need a variable which is our starting player and if no value is given it will be simply
‫true.
‫That means that our player one will be the starting player.
‫So that means the ball will go from left to right.
‫All right.
‫And what that one should be called start ball.
‫Now, we still get the red underline.
‫That means that we still have an error here.
‫That is because we don't return something.
‫Not all cold puffs return a value, so let's return some values.
‫What we need, therefore, is, first of all, we need to reset our hit counter.
‫So we set that to zero here and then we yield return a new weight 4 seconds.
‫So that means we make our game.
‫Wait for 2 seconds.
‫So that means whenever we start the game or we whenever start ball is called, we wait for 2 seconds
‫after we set the hit counter to zero and then we start the movement.
‫So that means we check it's the first player starting or is player to starting.
‫So this player is starting player one.
‫If that's the case, go ahead.
‫This dot move ball.
‫So we call this method here and we give it a vector of new vector two with the value minus one and zero.
‫That means the ball will move to the left and otherwise the ball should move to the right.
‫So this dot move ball new vector two now it should move to the right.
‫So one and zero.
‫So one is the x value and zero is the Y value as you can see, float y.
‫All right, now that we have that, we only need to start a CO routine.
‫So start CO routine and in here we call our this dot start ball.
‫All right, now let's save that.
‫We will need to add some further functionality to the game later on.
‫But for now, let's test that within unity.
‫So what I need now is to change the mass of our ball.
‫It should be super lightweight.
‫So I'm just going to use 0.01
‫and for the gravity scale, I'm just going to go for zero.
‫We don't need any gravity for our ball at all.
‫Now, as you can see here, the movement speed is at zero, extra speed per hit is at zero and our max
‫extra speed are zero as well.
‫And that's not something that we want.
‫Of course, we want our ball to move and a good starting value is 400.
‫A good extra speed per hit is 50 and the maximum speed should be 1000.
‫So these are just values that I have come up with after testing.
‫And let's see what happens after 2 seconds.
‫Our ball starts moving to the left because it's player one's turn and then it goes back and forth.
‫As you can see, the speed is very equal, so it doesn't really change.
‫It's always the same speed even though we have added this extra speed per hit value.
‫So let's go ahead and increase the extra speed per hit with its hit counter.
‫So we need to add a method which is increasing our hit counter by one and I'm going to call that one
‫public void increase hit counter.
‫So what that will do is it will increase the hit counter by one.
‫So it says if this dot hit counter multiplied by this dot, extra speed is smaller than this dot max
‫extra speed.
‫Then please go ahead and increase the hit counter because otherwise we would have a speed of well at
‫one point and unlimited speed.
‫So it will go up, up, up and up and further up.
‫All right.
‫That that method is not called at all now, and it will not be called within our ball movement script.
‫We will call it, however, in the script for our records.
‫So we're going to do that in one of the next videos.
‫But so far you have seen how to add movement to your ball, and we will go on with that in the next
‫videos.
‫So see you in the next video.
‫Before we do that, of course we need to clean up, so let's drag that ball movement into scripts and
‫let's save our scene and see you in the next video.