2D Camera in Unity + Confining to bounds [Cinemachine]
Create the perfect 2D camera in Unity. Cinemachine gives you AAA quality camera controls within seconds. Creating your own camera script, although a viable option, can be very time-consuming and unless your camera controls require something completely unique, Cinemachine will give you what you need.
in this video i'll show you how to set up a quick 2d camera with a few tips and tricks as well as confining your viewport to the relevant play area okay so first thing you need to do is window package manager and then just ensure that cinemachine has been installed all right next go to the cinemachine tab up here and click create 2d camera what this will do is it will create a what's called a virtual camera on your scene and effectively what this does is it's it replaces your camera transform kind of so if you'll see here i've got my virtual camera selected when i move it around my actual camera itself is pinned to it okay so all we need to do to get this working and following our player is simply drag on our player into the cinemachine virtual camera follow slot now if we press play you'll see it's already giving us pretty pretty nice desirable results so there's a few things we can do with this for example over here on the x-stamping and wide dampening let's change that to five and if we go back in game we'll see it adds a bit more of a floaty feel to it kind of looks a little bit cinematic next i'm sure you noticed these red borders here so that's called the dead zone and it will only show if you've got your virtual camera selected but the way that works is if we just go this way you'll see that the character will never be allowed to leave your confines like that another cool feature of cinemachine is the look ahead time so if we just go put this to about four add a bit of smoothing to it now you'll see when i run you'll see the camera is kind of like looking ahead of where we're actually going so obviously if you've got like a fast-paced game and you're running to the right you you want the player to be able to actually see what's coming so cinemachine can easily handle that for you all right so what if you don't want to show this much of the level all at once so what we can do is let's just reduce the size of our player here a little bit and let's zoom the camera in just a tad just like that okay so now now we don't have the full level in view so that's all right but what about all of this blank space here that's not really helping anyone what we can do is we can confine the camera to a certain area okay so this is incredibly handy so with your virtual camera selected scroll down the bottom and you'll see this extensions part here if you click that and you add a confiner in so what this is going to do is it's going to confine where the camera can actually travel alright so to give the confiner plug-in what it needs i'm just going to create a child of this level and call it confiner and then i'm going to add a polygon slider 2d to it and then click the edit collider button and then i'm just going to drag this around my level roughly to where i want the camera to be able to see to with a little bit of padding there and i'm going to make it a trigger just so it's not colliding with everything in a scene and then on my virtual camera just slide in the confiner okay so let's give that a go i'll full screen it add a little bit of floatiness to the camera let's try it out and yeah there we go so it's keeping the camera um on the player and it's following it but it's not allowed to go beyond the bounding box and show all that useless stuff it's keeping more of the actual relevant parts of the screen of the screen in view so i hope that helped uh if it did leave a like subscribe and i'll see you next time for another tutorial you