Smooth Camera Follow in Unity - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna have a look at creating a smooth camera follow script this is something that I see a lot of beginners having trouble with a lot of people either get weird controls or jittery movement and are just confused about the whole situation in general so hopefully you will find this video useful also special thanks to Commander Firestone 38 for his support on patreon if you want to support these videos you can go to patreon.com/scishow started so have this scene setup with a simple cube that we can control around the platforms but the camera of course is currently stationary let's change this by selecting the camera going at component and we want to create a script called camera father it's 2 C sharp and it create an ADD let's double click it to open it up in visual studio now first off we can go ahead and delete the two using tags at the top and we can also delete both our methods the first thing that we need is a reference to what we want to follow in our case that's going to be the player let's go ahead and create a public transform remember we use transform whenever we want to get information about position rotation and scale and we can call this variable our target will also need a variable that defines how quickly our camera will snap to the target because we'll be smoothing out our camera movement we need to be able to adjust the smooth amount so right below this will create an oddly public variable this one is going to be of type float and let's call it our smooth speed so the higher this value is the faster our camera is going to lock on to our target and so the smaller it is the more time we are going to spend smoothing you want to place this somewhere between 0 and 1 I'm just going to fold it to 0.125 so let's start locking onto a target we want to do that inside of a method so let's go void and this is where I see a lot of people going wrong especially people new to unity because the logic here goes well I want to snap my camera to the target and I don't want to do this only in start because if our target moves well then we want the camera to move with it and so the reasoning goes let's do this every frame and so people write update here the problem with this however is that our targets position is often also edited in the update method and so we get a situation where the target and camera is kind of competing to go first and that results in weird jittery behavior what we can do instead is actually change this to something called late update late update is the exact same thing as update except it's run laughter and so now we know by the time that this function is called our target has already done all its movement so let's test this out by first setting transform position our cameras current position equal to our targets position so target position and when we now save this and head into unity we can see our two variables appear in the inspector let's take our player and drag it into the target slot now when we hit play we should see that our camera snaps directly to the position of our player so our camera is currently inside of our player this is not really something we want we definitely want to offset the position of our camera but you can see that it does follow the play around and that everything feels very smooth so let's go and add an offset let's create our offset as a variable so that we can easily tweak it in the inspector will create a public vector three and the reason that I'm creating a vector three here is that I want to be able to offset our camera on all three axes want to be able to pull it back on the Z move it up on the wire a little bit and maybe even play with the X let's call this variable offset and we don't need to put in any defaults here then down here where we set our current position equal to our target position we can actually just add our offset onto that position so we simply write plus offset if you're unfamiliar with vector math this might seem weird we're not adding two numbers here we're adding two vectors but this actually works out in this simplest way possible when adding two vectors together we simply add together each component so we add the X of our target position with the X of our offset and the same thing with y and z so now when we save this and go into unity and hit play we have an offset variable and we can control this at run time so I'm just gonna take my Z value here and bring it back and you can notice our camera zooming out I can do the same thing with my Y and with my ex I'm gonna set my X to 0 Y to 2 and Z to negative 10 we can then right click on the component hit copy component exit play mode right click again and hit paste component values and now when we play we should actually already have a nice camera follow script but we have just a few things missing we still need to make our camera smoothly lag behind our player and we also need our camera to always face the player because right now if I increased in Y well then we lose track of our player to smooth out our movement let's take the position of our target that's the offset and store this in another variable will make this of type vector three and we can call this desired position and so if we don't want any smoothing we can simply set transform drop position equal to desired position directly and if you want to smooth we simply add another step in here we create another vector three we call this the smooth position and set it equal to vector three dollar lope stands for linear interpolation and is the process of smoothly going from one point point a to another point B so this method first off takes in our current position which is our starting point for that we'll right transform dot position it then takes in our desired position our end point let's put in our desired position and finally it takes in a float called T it's called T here because we often want to loop over time but T is actually just any value between zero and one when it's zero this method is going to give us the first position which is currently transformed our position when it's one it's going to give us the second position which is currently desired position and if it's anywhere in between well then it's going to give us a mix of the two so what we want to do here is actually input or smooth speed having then set our current position equal to our smooth position so what's happening now is that every frame we get the position that we want to snap to we use vector 3doodler to get a bit closer to that position and how much closer we get depends on our smooth speed if our smooth speed was one well then we would move the entire way on one frame if it was 0.5 well then we would move half the way each frame if this is hard to wrap your head around I totally understand it definitely was for me at first I think we should just try it out in unity and you can see what it looks like now depending on the way that you're currently handling movement this might turn out really smooth or you might get a bid of triggering motion I've searched around the farms trying to figure out why this happens according to unity documentation late update should always be called after everything else I did however find a solution to this if we go into visual studio we instantly go in and replace late with fixed update and so now we're actually updating our position in the fixed update physics loop don't ask me why this works but when we now hit play you can see just how smooth that is if anyone has a good explanation for this please post it in the comments finally we can have our camera always look at our player this is super simple to do we simply want to call transform look at and the object that we want to look at is our target this way unity will do all of the rotational calculation for us let's say that go into unity hit play and voila a smooth follow script it's working and we can see just how easy it is to update the way that we view our player there of course a million ways that you can expand upon this system if you want you can also pick up a camera solution from the unity asset store some of these allow you to do much more advanced camera stuff like having multiple targets setting up framing camera shakes transitions all that stuff I'll have some links in the description that's pretty much it for this video I hope you enjoyed it if you did make sure to subscribe so you don't miss the future one also if you have any questions you can go to from Rockies comm thanks watching and I will see you in the next video thanks to all the awesome patreon supporters who donated in May and a special thanks to Derek huge jerk face of Mara Phi Stone gamer Cmdr Firestone 38 Thomas voice james calhoun cyborg mommie adjacent Lotito if you want to become a picture in yourself you can do so at patreon.com slash brackets
Info
Channel: Brackeys
Views: 660,065
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, smooth, cam, camera, follow, laggy, jerky, smoothly, following, player, script, scripts, code
Id: MFQhpwc6cKE
Channel Id: undefined
Length: 7min 51sec (471 seconds)
Published: Wed Jun 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.