OK, so to get the camera movement happening, it's actually quite simple. So where we left off is when we play the game, our ball moves forward, we can move left and right with the controls the keyboard, and we can also do it on mobile. And but the camera doesn't actually follow the player. Now, this is obviously a problem. So there are a few ways we can handle this, we could actually just drag the play out. We could just start drag the camera and nested within the player, press play.
And it does something similar to that. And of course, we could combat that with code or just you know, make it you know, you could do some other stuff, but we're not going to do that. So we're just going to understand, we could go to Window, we've got a package manager, and we could actually click on all and we could download a package that there might even be included in your version of you leave here if you're watching this feature called cinema machine. So the machine used to be a paid actor. In the Asset Store, but now it is free for all unity developers because Unity has bought out the program because it was such a good plugin. It is the bee's knees if you want to make a camera for a 3d game, like God of War cinema machine if you want to make a game like Mario, and you need a camera for just a simple 2d game like that cinema machine, if you want a camera for a game like Gears of War, it shakes and or it looks really awesome.
It has some really cool cutscenes cinema sheen. It is extremely impressive and also extremely free, which is sweet. We could install that and if we wanted to, but for what we're doing, I don't actually think we need this much like grunt This is like putting a VA engine on a say tricycle. You don't really need it. Because all we're doing is moving the camera in one direction where it's not going to be doing anything else and it's actually really simple to do that. So we're going to do things the old fashioned way cuz I It'll also show some really cool some some really basic coding techniques that may help to understand things a little bit better.
So we're going to go over to our Scripts folder, we're going to right click, we're going to create a new C sharp script. And we're going to call this camera follow. They're gonna open up this. So there's a few things we need. The first thing we need is, we need to tell the script what we're trying to follow. So we're actually trying to follow the player.
And how do we know where the player is? Well, we know where the player is from the players transform position. So right now it's at 00, negative 41 45.1. Right? So we need to get that information. So we need a variable that can help us find information.
So we can just write public game object. So we're creating a game object variable. I'm just gonna call it player We don't need a stop method. So we can just get rid of this. don't even need this line of code here. It's just a comment, make our code extra lane.
And we're going to change update to late update, which I find works a little bit better for things like cameras, it's basically happens after update is called late update gets called. So we just go into another big one big long line of code. But hopefully this one isn't as confusing. It's very similar to the other line of code we wrote in the player script here, but it's just a little bit different, because we're not actually clamping anything. So all we're doing is we're saying, hey, this game object, sorry, this. Yeah, well, this camera just needs to move in a certain direction.
And so we want to smoothly move from its position and is now to the players position minus a certain amount of like, you know, distance between The camera in the player. So we could run game objects dot transform dot position. So this is talking about the main cameras transform position, which right now is this, this is the vector three. And we're saying gamma transform the position, remove that the common that was a mistake equals a vector, three dot loop. Okay, once again, looping is just smooth motion from one position to another. So we need a vector three a vector three B and how long it's going to get there.
So our first vector three position is just where we are now game objects dot transform dot position. And the next place we want to be is we could just write player position. But the problem with that is, well, I'll show you actually, let's run player dot transform dot position. Time Delta time, times 100, just to get the ball rolling. Cool. So if we press play, and we go to our main camera, and we drag our camera folder script over to the camera is going to actually ask us what the hell's the player.
I don't know what this is. So then we need to actually drag the player into that section. Camera for. So now, the script knows where the what the player game object is. And it's saying, hey, make my position. Lip it so smoothly move from my position to the players position and do it at time but over time, times 10 times 100.
Sorry. So let's see what happens. Press play. Oh, that's interesting. What's going on here? Okay, if we pause it and we zoom in, you'll see The map player is here for fun.
And our camera is actually right in the middle of the player, because we've just looked to the exact middle of the sphere. So things aren't working out exactly how we want it to them. So what we actually need to do is we need to create a bit of a buffer zone between the player and the camera. So how do we do that? Let's get back over to our script. And we're going to delete all this up to the vector three dot loop, open parentheses.
And we're going to say game object, dot transform position. This is what we want to look from. And we want to look to a new vector three. And the new vector three will equal zero. on the x axis, the wax will be damaged or transformed opposition dot y. So basically, don't worry about changing anything for our Hot for the camera hot.
Don't worry about changing the camera height, just keep it what it is. And the last one is player dot game object dot transform dot position dot z. And here's the here's the tricky bit minus 10. That's a pretty good distance run. Well, I think so. So basically what that means is, let's just end sorry, we'll just finish this on the code actually.
And then put a comma there and write time, dot delta time. times 100. just seemed like a good number before. Just so just bear just is there so it speeds it up. And with the end with parentheses, close the semi colon, one line of code. Let's see how it goes. So if we press play, Cool.
So our cameras, many don't even look. Yeah, there we go, you can see our camera is actually moving, then we can move left or right. And I can it's gonna fall off the edge. But if we press play again, you'll see that the big move left and right on the screen, and things seem to be working out pretty well. So, right now, if we get the player and we put the playback at zero, and we put the camera back at, well, it can stay at that height and we put that back at zero. They're both on top of each other, but it also drag this back to negative 10.
That would, that would mean that that's exactly if I press play, it should just start rolling on from exactly where we are because you know screen Script, that's actually what we've said, we've said, make a game on character make position, sorry, let from our position. And I'll just zoom out a little bit, make a position, look to a new vector three, the x position, don't worry about it, just leave that zero. The y position, don't worry about it, leave it at the default of whatever the cameras position is. And as the position leave it at the player's position, but minus 10. So if I made this 15, it obviously go back further, if I made it five, you'd go back further and do that at a rate of time. But though time times 100 awesome.
So now we have a working game in the fact that our working movement system in that in that our player can move left and right and continue on and he won't go off the screen. Obviously, if it's falling down now, and the camera will follow him or her soon the next video we'll do a little bit of a test.