Hello, in this video we're doing something really cool.
We're enabling an autoplay so that the game plays itself.
This is great for testing so that we can bump up the speed and
test out our game without having to rely on Rick's terrible skill for
being able to move a mouse and bounce a ball.
Okay so let's jump in and get started.
Let's start by getting clear on who is doing what and
what is doing whom and how and all that kind of good stuff.
So I believe that we should have somewhere on our GameSession
that allows us to say yes or no in terms of autoplay.
GameSession is the logical spot for it cuz this is what is controlling the game,
or not controlling the game necessarily but
keeping track of what's going on with the game.
So that's where we'll put that on our GameSession script.
And then in terms of the paddle, what's the paddle doing?
The paddle needs to know where am I moving, where is my current trajectory,
where is my current velocity, I should say.
And that velocity is going to be looking at either the mouse.
Can I draw a mouse here?
I've got my little thingy on there, so it looks like a pointer.
Okay, that's the mouse, [LAUGH] terrible mouse.
Or it's looking at the ball, so I was looking at the ball or the mouse.
And that is owned on the paddle, is making that decision.
And then the ball is just being cool and saying, hey dude, I'm a ball.
Okay so [LAUGH] let's jump over, clear my screen.
Jump over, first of all, into our Paddle.cs.
We'll be working in Paddle.cs and GameSession.cs in this lecture, maybe
having a little bit of a look at Ball.cs, but for now Paddle and GameSession.
When we look at our code we can see under update we wrote this code awhile ago,
we're looking to say, where should we move the paddle?
And this is a cool way of saying we should move the paddle
based upon where the mouse is.
So, the main thing we're looking at here is when we say the paddlePos.x =
Mathf.Clamp.
That's just a way for us to say that it doesn't go off the left or
the right of the screen, here's the left and the right of the screen.
This is it right here, mousePosInUnits.
So, go for your X position, wherever the mouse says you should go.
What I wanna do is create an if situation, a conditional, where we say.
If autoplay is on, then this should be wherever the ball is.
And if autoplay is not on, then this should be wherever the mouse is.
So underneath our update, let's give ourselves an extra couple of lines and
start by typing private.
I'm going to make this a float, and
this is a good chance for us to practice return types.
It's been a while since we've done this, but the return type of float,
that means that when we run this method we're creating a method at the moment,
it's going to return back to whoever called it.
It's gonna return a float, and we'll call it
GetXPos and open and close our parentheses.
Now, what is in here?
There's a few things, we'll start writing it out and we'll have to jump around
a little bit as we realize we need to create some new things.
So we'll say, if, I'm gonna write it the long way, and
then we'll factor it down in a moment.
If find, wrong way Rick, find object of type, and what type are we looking for?
Well we were looking for the GameSession at this point.
So we wanna know,
cuz the GameSession, we said this is where we'll be controlling the autoplay.
If (FindObjectOfType(GameSession)) what?
We're going to say IsAutoPlayEnabled().
Okay, we haven't created that yet, so we'll go off,
we'll jump off into GameSession and create what this means.
Basically we're looking for a ball to be returned that's either true or false.
In other words, yes it is, or no it is not enabled.
So let's jump over into GameSession up at the very top of our script up here,
we'll SerializeField.
This'll be of type bool, so bool is going to return true or
false, so a bool and we'll call this isAutoPlayEnabled.
And that's going to allow us in the inspector to toggle on and
off that is autoplay is enabled.
Now we need a way of allowing Paddle.cs to know what the situation here is.
If we were using a public variable it'd be a little bit different.
We could be accessing that from paddle, but
then paddle could also be changing that.
So we get into that situation where we don't want anything else, except for
the clever player, in the inspector,
to be able to change when we have is autoplay on.
And that's the only time we want to be able to change that, so
this is why we're doing it as a serialized ball that we're gonna return,
rather than a public variable.
Now, let's scroll down to the bottom of our GameSession script.
We'll create a public method.
And this public method is going to be returning what?
It's going to be returning a bool, and we'll call it IsAutoPlayEnabled.
Okay, now here's a little mini-challenge for you.
I'd like for you just to complete the sentence in here,
the will return back to paddle.
It will return what we want it to return.
Okay, if you've been following along, and
I know it's a little bit tricky if it's a bit new to you.
But if you've been following along, you might have got that.
Otherwise, we're going to type in return.
And this is our variable, is autoplay enabled.
So with our method, our convention is a capital i and
with our variable our convention is a lower case i.
So we're returning this bool, whether it's true or
false, anytime something is calling this particular method.
We're about to call this method over here in Paddle.cs so we're first of all
finding objective type GameSession and then we're calling is autoplay enabled.
Okay, and then what do we do if it's enabled?
Well, curly braces in here, if it's enabled we want to be
feeding into this spot just in here that I'm gonna change right now to GetXPos.
So this is our update method for our paddlePos.x.
We're gonna change that to GetXPos and
we need our parenthesis because it's a method.
And we want to be returning.
When we called that we want something to be returned back into that place.
What are we going to return?
Well, if the autoplay is enabled we're going to return the position of the ball.
Okay, so how we do that?
Well, we'll do it the long way first of all and then I'm gonna factor out a little
bit, FindObjectOfType, we're looking for what, for ball, Dot,
well we need to find where it currently is on the x axis.
So that's going to be transform.position.x, semicolon.
Okay and then what do we do if the autoplay is not true?
So if autoplay hasn't been turned on then, or
in other words if autoplay is false, then we will put in an else.
We're gonna return, well I pretty much going to return what we were using
here in the first place which was mousePosInUnits.
We don't need to call it this variable anymore because we're calling GetXPos,
sorry, GetXPos is a method.
And then we wanna return the position of the mouse.
So I'll highlight everything after the equals sign and float mousePosInUnits.
So everything Input.mousePosition, down to screenWidthInUnits.
And the semicolon, I'm gonna copy that for the moment and
then paste that after my return.
Now, back up into update, that looks pretty good.
Now I'm gonna delete it.
I like to do it this way so you can see exactly what I'm doing so
things don't disappear all of a sudden.
Now, if we look at all of our logic here, we're saying the paddlePos, that's cool.
We're defining that as a variable.
paddlePos.x is going to be GetXPos.
What is x position?
Well, if autoplay is enabled then it's gonna be wherever the ball is.
And if autoplay is false, so it's not on,
then it will return the mousePosition as we were doing.
Let's save that, make sure that everything is working, jump back over into unity.
We should see if we're on game session,
we should see in a moment that it will give us, did we save our GameSession?
Let me just double check.
We didn't save GameSession, make sure you do that.
Back over into Unity, in a moment we should see an autoplay option.
It takes a moment for, there it is, autoplay is enabled.
Let's play to make sure it's all cool at the moment.
Okay, the ball moves, the paddle moves, we can play, yes.
And then let's turn IsAutoPlayEnabled, we'll turn it on and so the answer is yes,
it is enabled.
And, Okay, so now the paddle's not moving, interestingly.
But when we click Go, it goes.
Why is the paddle not moving?
Well, it's linked to the ball and the ball hasn't moved yet.
Okay, cool, so that's exactly the functionality that we wanted, success and
high fives all around.
I'm gonna do one last thing in here, I'll set it to you as a challenge without
a slide, just a challenge that you and I talk about, just a secret between us.
And the challenge is to factor out the FindObjectOfTypes.
We've got two of them in here, and if you had to look closely,
GetXPos is sitting within out update.
So that means every single frame we are calling our GetXPos, and
therefore we're doing this FindObjectOfType.
On a game our size, not a problem but
this is one of those things you'll read in Unity documents.
It says FindObjectOfType is a bit expensive so
try not to do it in your update.
So I want you to factor this out so that we're not calling,
we're not trying to find object of type every single frame.
Okay, so pause the video and take that on now.
Okay, and we've done this a couple of times.
So should be reasonably apparent when I explained it to you here,
if you haven't done it already.
Hopefully you did give it your best shot, is we need to be caching our reference.
So, cache references.
The first one I will say is, I call this, sorry.
This is gonna be GameSession, not sess gaming, GameSession.
Rick can't type.
I'll call this theGameSession.
And then we want one other, which is Ball, is the two things we're looking for.
And I'll call it theBall.
Now in our start method this is where we find the reference.
This is the one time we say, go get it, go find this for me.
It sounds like we're talking to our puppy out the back.
Come on, here boy, go find the GameSession [LAUGH].
Go get the ball.
Now we will say, theGameSession =, what?
It equals FindObjectOfType<gamesession>.
Open and close parentheses and semicolon.
And then you should be able to do the next one pretty easily,
it is pretty much the same.
theBall = FindObjectOfType, and the type is Ball.
Open and close parentheses.
Now, when we start up our script, start up our game,
it's gonna cache those references.
So in our GetXPos, we can jump down in here and just say,
instead of saying FindObjectOfType, we've already done that and
we've stored that in the variable of the GameSession.
So we'll say the GameSession.IsAutoPlayEnabled, and
then return, theBall.transform.position.x.
Save, jump back over into Unity, autoplay is enabled at the moment.
Click on the play button, any second now.
Often takes a while for my Unity to think.
Thinking, thinking, and then it can't move because autoplay's enabled.
Click play and it moves.
And the really cool thing in this whole process,
this is the one thing I wanted to show you, more than just hey it can play
the game without Rick being useless, is you can turn the speed up.
And then very quickly play-test your game, without actually having to sit there and
play it, and have two blocks remaining, and
then make a mistake where the ball goes flying down on the screen.
So you can turn up really fast and just to play-test, to make sure that there's
nothing going, so there's a few situations where the ball might get stuck,
cuz it's not hitting the side of the paddle.
And when we're playing, we can hit side of the paddle and get the angle.
So you could change up your paddle a little bit, if you wanted to.
But it's gonna show us things like what happens if we're on our 5th level and
we have a score of 99999 that clicks over, then maybe our score goes back 0.
And you're like, I wouldn't have known that if I hadn't have played the whole way
through all of my levels.
Okay and speaking of playing through all of your levels,
make sure that you test this, apply everything from your prefabs.
So, click on, if you've change anything, any settings,
make sure you've clicked on apply.
That is if you've changed something in your hierarchy, click on apply.
If you've been changing it directly into your prefabs, then it should be fine.
So, check your other levels.
Save it up, check your other levels and in the next video,
we'll be wrapping things up and finalizing our progress on block breakers,
so great work and I'll see you in the next video.