Transcript for:
Implementing Camera Movement in Unity

so today i'm going to show you how to do camera movement inside our 2d games now if you follow up my lessons up until now you should have the same thing as i have in front of me here the only thing that i did compared to the last episode was i changed the speed and jump force of my character and i just simply created a much better running animation at least i think this looks better than what i made in the previous episode now the way we can do camera movement because as you can tell when we're moving sideways nothing is really happening with the camera we're just sort of like in a stationary point and our character is just you know running around but what if i want the camera to move with the player then we can actually do that in a very simple and very easy way in unity we can now install a package that allows us to create a camera that can then follow the player and do it in many different ways that you know might be smooth or not so smooth we can like pan forward to see what the player is running it has a lot of really awesome functionalities so what i'll do is i'll go inside my package manager i'll go inside where it says packages in project and just click it because we want to install a new package that we don't already have so i'm going to click the drop down go inside unity registry so we can see all the different ones that we can install and then i'll just sort of scroll down and find the one called cinema machine so i'll click it and install it and once you have it installed you'll actually notice that we get a new tab up here in the corner where we can actually click something called cinemachine that allow for us to create many different types of cameras there's a lot of functionalities with this cinemachine camera and we're just gonna focus on the very simple follow the player one in this episode because otherwise this is going to be a very long video um so what we're going to do here is we're just simply going to go up into cinemachine and we're going to say we want to create a 2d camera once i click that you'll notice that we get a cmvcam1 i just go ahead and drag that up so it's right beneath my main camera and what this basically does is that it takes over the control for the main camera so the main camera is basically going to do whatever cinemachine's virtual camera is going to tell it to do which also includes following the player so now with the cinema machine created we can click it and then inside the inspector you can see that we have a couple of different settings here that we can mess around with to sort of stylize the way that we want the camera to behave as it's following the player so the first thing we need to do is we need to tell it what do we want it to follow in this case here i wanted to follow the player so with cinema machine selected i'm going to drag my player component inside follow and with that if i were to play the game you'll notice that we already have a camera working that follows the play around so you can see that when i jump it follows me up when i move it follows me on the platform everything is basically working just fine as it is right now so the first thing we're going to do is we want to make sure you can actually see what exactly the cinema machine camera is looking at and the way we can do that is by going up and say we want to activate the game window guide so by taking this one off you'll notice that when i start playing the game we get this grid system that actually allows us to see what the the camera is seeing on inside the game what you'll notice is that we have something in the center here which is a small yellow dot and that is actually what the camera is going to be looking at when we're playing around with the player so if we were to actually move you can see that the yellow dot moves with the player currently because it's just the default settings and the camera is going to try to center the camera on that yellow dot so when the the player moves it's going to tell it oh oh we need to start following the player and then it's going to have this little catch up system going now we will talk about the catch-up system because you'll notice that we're not staying on the player like very harshly and you know making sure that the player is always centered right now it actually allow for the player to run sideways a little bit before it starts catching up and that is something we can adjust in here so to start with we have something called look ahead time which is actually what allow for the camera to look ahead of where we're running so if i'm running to the left it's going to look ahead a little bit to see is there anything to the left that we're running towards which is a really neat thing if you're making a really big side scroller game where you need to be able to see enemies coming in fast if you're moving a certain direction so if i were to take the look ahead time and move it up to 1 you'll notice that when i start running the camera is gonna look ahead or where i start running towards of course now you'll notice that because i set it to one it's very harshly going to like run ahead of where the player is so you can see that the player is like shooting backwards because the camera's trying to look at what is in front of him let's say 0.5 and then let's try it out so now you can see it's actually following the like ahead of time uh of where the player is running in a in a you know not as harsh way now you will notice something here which is that it's very quickly looking ahead of the player which is not really the effect that we want we want it to smoothen out a little bit more so it's a little bit more you know not as stressful to look at so what we can do is we can actually smooth the look ahead time so i'll put this one to something like 10 or something close to it then you'll notice that it is smoothly trying to catch ahead of the player which is you know a lot less crazy to look at now we do also have something called look ahead ignore y which is something that is very useful because in a lot of cases i might want to have this look ahead feature but only going sideways and not when it comes to up and down so what i can do is i can tick this one off and it's no longer going to try and look ahead when it comes to up or down now we also have something called damping and damping is actually what you might have noticed if it were to just like deactivate everything when it comes to look ahead here so with the look ahead disabled and you'll actually notice that the yellow dot is moving ahead of the center and then the camera is trying to like smoothly catch up to it which is something we call damping so right now you can see we have a damping set to one one one uh for the different axes and we can actually go ahead and set this one to zero just to sort of test this one out so you'll notice that now we always stay centered when it comes to the player and then the further we increase these the more it's going to damp the movement and the more it's going to you know try and slowly catch up to the player so this is something you might want to mess with as well for now i think one one one is fine and then we have this green x and y center-ness center-ness basically what this allowed for us to do is if i were to say well you know in a lot of 2d games we don't have the player completely centered on the screen so in a lot of cases we actually might want to just kind of like have the play a little bit further down towards the ground so what i can do is just sort of like increase this a little bit so the play isn't completely up center on the screen and then lastly we have something called dead zone and soft zone which is also something you can just kind of mess around with in order to get the right feeling of the camera movement basically the dead zone is right now set to zero so we can't really see the effect of it but you'll notice that if i were to move my player around you can actually see that the camera is trying to catch up to the player immediately as i start moving but in some cases i might want to allow the player to move just a little bit to the right or to the left or upwards or downwards without the camera trying to catch up to him so what i can do is i can increase the dead zone so i can say you know what let's allow for a little bit more spacing before the camera tries to catch up so right now the yellow dot as long as it stays within the dead zone the camera is not going to try and catch up to it so i can actually start moving and you'll notice that you know the camera isn't moving with the player but as soon as i go outside it's gonna try to catch up to the player which is a really neat sort of feature to have and then we also have something called the soft zone so right now you can see that we have this blue box around and this is basically just the area where the camera should not allow the yellow dot to go to by any means necessary in order to test this one out properly let me go ahead and increase the ground width so we don't have it set to 20 on the x-axis but let's say 100 instead just so we have a little bit of space to run on and what i'll also do is i'll go into my camera and i just go and increase my damping by a lot so the camera is going to take a quite a long time to catch up to it and then i'll also minimize my soft zone a little bit oh and we need to make sure we don't have any look ahead time because that will actually go ahead and ignore the the soft zone so double check look ahead is disabled and then we can actually go ahead and test out this red soft zone as it's called and what you'll notice is that the yellow dot is not allowed to move into the red zone so if it were to start running you can see that the camera is gonna start to follow you know whenever we hit the red zone it's not gonna do any damping it's not gonna do anything with the dead zone uh it's just gonna say you know what you can't go into the red zone like that is forbidden and with that we basically talked about everything when it comes to this cinemachine camera at least a 2d camera and inspector settings so what i'll do is i'll stop playing the game and what you'll notice is that when you make any changes while you're playing the game which is basically testing the game uh it doesn't save any of the settings that you applied during the time that you played the game so you do need to make sure when you make changes inside the inspector that you do it when you're not playing the game that did kind of mess up my recording here quite a few times because i i stopped playing and i started playing again and i had to like trying to match up the settings again just remember your settings do not save when you're playing the game so what i need to do is i need to make sure we're not playing the game and now i need to start making my changes um so what i'll do is i'll just go ahead and say i want to have a small look ahead time of 0.5 i want the look-ahead smoothing to be five i want to have a little bit of damping so let's not do one let's do something like 0.6 and i also want to make sure that my screen is you know i want the player to be a little bit further down i don't want him to be centered on the screen so let's make that about 0.7 and let's go ahead and increase just a small amount of dead zone area so let's go ahead and actually go ahead and test this one out because we can actually change this without seeing what we're doing so i'm just going to increase my dead zone width just a little bit and a little bit on the y-axis just a tiny teeny tiny bit so something like 0.03 and on the width i want it to be 0.07 let's go and remember these settings because when i start playing you can see everything reverts back so i'll just go and change these to 0.07 and 0.03 and now if we were to play the game we should have something that i'm at least satisfied with so i can actually go ahead and run we can jump the look ahead time is of course not like i wanted to let's go ahead and decrease that one let's say something like 0.1 and smoothing to something like two and let's also make sure we don't do anything in the y axis when it comes to look ahead so with this i think we have something that is quite okay it is smooth it is like i wanted to i want my player to move around a little bit without the camera having to follow so with this we now have something that seems a little bit smooth when it comes to the camera movement um so hope this was useful i hope you understood everything when it comes to this cinemachine camera and why it is a awesome tool when it comes to camera movement that follows the player compared to like trying to hard code it and the best thing this is built into unity this isn't something that you have to download off the asset store or pay for it or something it's just something that is included into unity so why not use it so with this i hope you enjoyed this video and i'll see you guys in the next video [Music] you