[MUSIC]
Righty-o, let's make some magic.
Our goal here is to move our paddle and to do that,
we need to have our paddle linked to our mouse.
And I'm going to outline for you the problem, and the solution,
I've talked about this a couple of times, you need to break it down.
What's our design problem, or our design goal, and then how do we solve that?
So our design goal here, in other words,
our problem is to move the paddle on the x-axis in line with our mouse cursor.
That means that as I move my mouse left, move my mouse right,
the paddle is also moving left and moving right.
That's the challenge, that's the problem, and
our solution has a number of steps to it.
First of all, we need to find the position of the mouse as it moves.
So we need to say, if the mouse is here, what is the position on the screen,
if it's there, what is the position, and so on.
We need to convert the mouse's position into world units as
a proportion of our total screen side.
And then to have it in world units, why in world units?
Well, if we click on our paddle here.
We click on our paddle, we can see it currently has a position,
it's exposition right at the moment is 8.39.
That would be based upon wherever it's pivot point is, and the pivot point for
our paddle is right in the middle.
If we move it all the way over to the left, do-do-do-do-do,
you can see when it bumps to the edge of the screen, it's still at position 1.
That's because our paddle is 2 in width, and our pivot is in the middle.
If we have it at position 0, that is where it would be, and
that's a little bit half off the screen.
So we don't want that,
but that is just showing you that as we move our paddle left to right.
We need to know the number in the world units,
not in some other arbitrary kind of number.
And then final step is to update the paddle's position to be whatever
the mouse's position currently is.
So if our mouse is at position just here,
then our paddle needs to know, I need to be in position just there as well.
Okay, so that's our problem and hopefully our solution.
Let's click on paddle and
we want to add a component that we will call Paddle with a capital P.
Paddle, New script, Create and Add, wonderful,
it's popped up in Visual Studio, although I haven't opened it yet.
But I'm just going to drag my script into my script folder,
as good organized folks do, and Visual Studio is going to be crazy on me.
It keeps wanting me to open up, it's like, come on, Rick, hurry up and open me up.
Okay, Visual Studio, now's your big chance, here we go,
your moment in the spotlight, Visual Studio, okay, cool.
What we'll do first of all, is we want to, in our Update method, and
why the Update method?
Well, as we're moving around our paddle, every single frame we want to know,
where am I, where am I, where am I, where am I?
That's why we're using Update, so within Update, let's start off just by finding
the position in the x,y coordinates of the mouse.
Let's start by printing to the console by typing Debug.Log,
what are we printing in the console?
Well, we're going to start by printing Input.mousePosition.
That is how we can see exactly where the mouse is positioned within our x,y
coordinates.
Save that, back over into Unity,
we can open up the console in this case, so I just click on Console.
Crocky and then click play, I'll expand my game window a little bit so
we can see what's going on here.
Expand the game window, oops, and
of course we're going to have Game Over as soon as my mouse hits the lose collider.
For now, I'm going to click on Lose Collider and toggle it off.
So once I click off, that means that will no longer take place.
The ball will just go flying past and not actually touch the lose collider,
because it's hidden for all intents and purposes.
Okay, click play agin.
Now you can see updating down here in my console,
when I'm within my game screen here, which is along here.
Bottom left, we should be somewhere around 0, we've got three numbers there,
0 all the way over to the right, cool.
So the first number is our x-coordinate, the second number is our y-coordinate.
And the third number is the zed coordinate,
which should be 0 because we're not going into and out of the screen.
So it's giving us some numbers, as we move up, the x is moving up,
as we move up vertically, then the y is moving up and down.
Cool, so we know where the mouse is on the screen, very cool, what's the next step?
The next step is for us to say, what is the mouse position just on the x-axis?
And the way we do that is by saying .x, that's narrowing it down,
we could say .x, or .y, or .zed.
We're saying, in this instance, we just want to know,
in terms of input, the mouse position for the x-axis.
Save that, back over into Unity, now we should be printing out just one value
instead of these three and that should be the x-axis.
Click on play, wait a moment while it's thinking, okay,
we've just got the one number.
And you can see as I go up and down, if I can keep it very still on the x as I
go up and down, it's not really changing very much.
It's staying in the very low 1, our single digit point something,
if I head over to the right, then it goes up to about 700.
Okay, that is very excellent.
Now, what next in terms of finding our mouse position?
Well, we want to know if our screen size was different resolution.
We want to know where we are relative to the overall screen size without having to
know, this is a 1080 width.
Or this is a 600 width, we don't want to have to plug that into the system,
we just want to know.
So the way we do that is we take our mouse position on x as a proportion of
the overall screen width.
And we have a mechanism to understand that which is saying
our Input.mousePosition.x/Screen.width.
And that's how we access the width of the screen.
The width of the screen will be, you can see here,
current width of the screen window in pixels, and we save this.
Back over into Unity, click on play, and as I move,
it should be giving me a value from 0 up to 1.
Okay, so we now have this as a percentage, if I'm right in the middle,
it should be around about 0.5, okay, a lot more useful.
And the final step we want to do in understanding where our mouse is,
is to say, well, we know that there are currently 16 units.
This is the way we've set up our world, so
that we have 16 Unity units from left to right.
That's why we setup our camera, if you remember.
Click on camera you can see a size of 6, which is the vertical.
That means that total vertical is 12, we have a 4, 3 aspect ratio, so
that means our width, given that our camera is 6, our width is 16.
Half of our width is going to be 8, okay, we covered that in a previous lecture, so
I'm not going to go too slow on that.
Hopefully, it still all makes sense to you, and the way that we say how's our
position relate to our units is to times this by 16.
Because that's how many Unity units we have from left to right.
Now, alarm bells should be going off.
We never want to put a number unless it's a 0,
a 1, or a 2, because they have a specific meaning or a purpose to them.
We never want to put a magic number like this right in the middle of our code.
So a mini challenge for you,
I want you to serialize our screen width in units as a variable.
So that we can use that variable instead of using the 16, so
mini challenge for you, pause the video and give that a shot.
Okay, so at the top of our script here, underneath class,
we are going to type SerializeField, SerializeField.
We need to make this a float because we have decimal points whenever we have dot
something, something, something.
We need, which are decimal places, [LAUGH], in other words,
just any small places, we need to make it a float.
And I'm going to call mine screenWidthInUnits, so
it's super duper clear exactly what's going on, and I'll initialize that at 16.
So, down in our print statement,
down here we will say, screenWidthInUnits.
Okay, and it knows screenWidthInUnits, what's that?
That is 16, and we save this, jump back over into Unity,
we can see that when we find our paddle.
Which is currently where our paddle script is.
We can see Screen Unit in Width, 16, and if we happen to change that,
we can simply go in and change it in here.
If we thought, our camera need to be a little bit further out,
then we can change it pretty easily.
So let's just have a look at our coordinates now to round out our section
of talking about coordinates.
From the left it should start off at 0, as we move to the middle,
it should be about 8 at ease.
And then over to the right, it should be 16.
While I'm here, I'm going to turn off my Next Level button,
I don't think we're going to need that.
Now that the ball hits the lose collider,
we can get to the Next Level pretty easily.
So within Canvas, it's still called Success button,
naughty me, I'll just toggle that off for now, just in case we need it later.
Okay, click on Save, looking pretty good.
The next part of what we want to do is to actually move our paddle to a different
position on the screen.
First step we need to do in that,
which is to just move the paddle around somewhere else.
Okay, currently the paddle is at the 6, around about 6, and around about 0.3.
I'm just going to get my y-axis here to be exactly where I want it, 0.25.
Okay, so it's sitting nice and flush on the bottom of my world, that looks nice.
The x-axis doesn't matter so much, but for illustrative purposes, we'll put it at 5.
So our position is 5 and 0.25 currently for the paddle,
now back in our script, let's have a look at this.
We want to, under Update, we need to move our paddle to a different spot,
and the way we do that is with a Vector2.
So what I mean there is Vector2, what is a Vector2?
I hear you ask, a Vector2 is a compact way of storing x and y coordinates,
that's how we'll be using it, which x and y coordinates?
Well, if you look at the position, all game objects have a position,
they have an x, a y, and a zed.
Now, because we're doing a 2-D game, we don't need to know zed, it should be 0 or
should be unchanged.
So instead of having a Vector3, which would use x, y and zed.
We just need a Vector2, which is going to store the x position and the y position.
X position sounds funny if you say it very fast,
it sounds like the paddle is running around getting its clothes off.
But not that sort of x position, so we need a new type of Vector2,
I'm going to call this paddlePos.
We could call it paddlePosition to be through,
but I'm going to call it just paddlePos because we know what that means.
Save us a couple of letters because it's still very clear,
pos is the universal way of saying position.
Equals, we have to use the keyword new, so it can construct a new Vector2.
We need to start off by saying paddlePos is a variable,
it's typed Vector2, and what are we going to say the x and y values are?
Okay, so new Vector2 that is how we construct this variable of type Vector2.
Vector2 paddlePos = new Vector2, and
then in brackets we're putting in the x and the y coordinates.
And we need to use floats, so for here I will say, our value for x is, say, 10f.
We put an f whenever we use a float,
we have to put an f on the end, I didn't mention that at the top here.
But a float has to have an f after the number, comma, and for our y,
what should we do?
Well, we'll say 4f, okay, so here we have a new Vector2 that will
be in this position, we're saying, go to this position here.
And how do we do the go to?
Well, specifically we say the transform.position
= what do you think it equals?
It's going to equal this variable we just created which is paddlePos,
now why is it transform.position?
If you have a look on your game object, you'll see that the game object here.
The paddle game object which we currently have our script component.
So our script is talking about it, saying, I am currently a component of this paddle,
and this paddle has a transform component.
See at the top here, transform, and within transform,
we have a position, so we're referring to the transform.position.
And when we're saying, what are the current values of transform.position?
We're defining them as paddlePos, what is paddlePos?
PaddlePos is currently a Vector2 of 10 and 4, so therefore
we're saying the transform.position will be 10 and will be 4.
Okay, let's save our script, and
make sure that that actually is the case when we click on play.
That our paddle should go blink, up to 10 and 4 on our screen.
And it does, you can see that it's jumped up to a value of 10, and
jumped up to a value of 4.
So we're controlling our paddle's position within the world from our script,
excellente, now what do we need to do?
Well, I'm going to show you one more thing, which is how we can say,
use the current position that you're in now.
Use where you are now, so when we say our Vector2 paddlePos = new Vector2.
And we've put in very specific values in here,
we would type in instead the transform.position.x.
Remember the first value here is an x and the second value is y.
Now, what this will say is wherever this particular game object currently is for
its transform.position.x.
That's what I want you to make the new Vector2 as, so in other words,
stay where you are on x and then move to 4 on the y.
Save that, jump back in here, click on play.
It should just move up but stay on the same horizontal axis, excellent,
as it's done, and nicely, it's juggling the ball there.
Very cool, and so there's one final step, and
that is going to be your challenge for this lecture.
The challenge is to move the paddle, set the paddle's Vector 2 so
that it is on the x-axis where the mouse currently is.
And we've set that up as a variable, and on the y-axis,
where the paddle already is.
Give it your best shot, and
I'll see you back here when you have given it your best shot.
Previously, we were just printing this out,
we were printing out where the mouse currently is.
Let's make that into a variable, so then we can use that variable for
good rather than for evil.
Let's call this variable, it needs to be of type float.
And we'll call it mousePosInUnits,
just to be very clear that it's to do with the screen units.
Hence this last bit here, it has to do with the mouse position.
And then we need to remove our parentheses that we had around our debug code
just here.
Save, we've got all of our framework in place, what we need to do in our Vector 2,
which is updating on every frame.
On every frame, we want to say that the x position should be our mousePosInUnits.
So, we want it to be wherever the mouse currently is, that should be x for
the paddle, why is it the paddle?
Well, this script, this class is currently a component of the paddle,
that's why we're moving the paddle.
And we're moving it down here with the transform.position of the paddle.
And then the y value in here, we want to have the transform.position.y,
to say, stay where you are on the y.
Let's save, back over into Unity, click on play and then,
move my game window and oops, I missed the ball as it went flying through.
But you can see that as we move our mouse,
the paddle is lining up exactly with where the mouse is, so, success.
We've taken the import of the mouse and
we've used that to move a game object left and right on the x-axis, great work!
In the next video, we're going to deal with, how do we stop the paddle going,
woo, way off the screen?
Great work, and I'll see you very shortly.