 Hello and welcome to another IndiePixel tutorial. My name is Kenny Lambers, and I'll be your instructor throughout this course. What we're gonna do in this course is talk about how to make a top-down camera script, all right? So it's actually not too difficult. You'll find that it's actually pretty simple to achieve. Even though I do remember the first time I was, you know, trying to figure this out back in the day with Unity, I definitely over complicated it. So I just wanted to show you how you could really build up a nice solid top-down camera, complete with a custom editor inspector and some cool SceneView UI to allow you to edit it in the SceneView directly. So that's what we're gonna do in this course. We're gonna first stub in our C-sharp scripts. Then we're gonna code up the actual meat of the camera functionality, and then we're gonna go and create a small editor tool. All right, and this will allow us to take advantage of the SceneView GUI drawing inside of Unity. It's really cool. I always love to add those particular aspects to most of my components, so designers or other people can use the script really easily or the component itself. And then finally, we're gonna create a menu function for our camera just to make it really easy to set up, right? So people don't have to go and find the top-down camera script and drag it onto the empty game object and so on and so forth. It would be great if we could just select an object in the Scene that we want to be our target, and then we go up to a menu option and say create top-down camera. And then it just sets everything up for us kind of deal. All right, so without further ado, let's get going. All right, so here we are inside of Unity. You can see I already have a scene set up here. These are the assets from the Low Poly Style Desert Pack. If I were gonna go over here, it's a Low Poly Style Desert Pack from CH Assets. It's a pretty good little pack. I like the content. It's really great for these tutorials, these types of tutorials, because it doesn't over-complicate the scene, and I wanna focus on what we're trying to learn instead of a pretty scene or anything like that. So let me play just so we can see what we're gonna be doing here. All right, so we have the top-down camera right here. We're gonna build up this nice little system here for the component itself. And it basically just allows you to change the distance and the height of the particular camera. So we're gonna walk through how to do all that stuff. So let me hit play. All right, so now when we drive this tank around like so, you can see that we have this really cool top-down camera type effect. I like it, it's not super complicated at this point, but it's a cool little effect and I love these top-down type of games. So while I develop this little tiny game here, this tank game further, I'm gonna be posting more videos on YouTube about each of these different components like the tank controller and the targeting and firing of the weapons and enemy AI, all that cool jazz, all right? So that's what we're gonna do. We're gonna make this cool little top-down camera in this particular tutorial series. All right, so I'm gonna hit save. So what we're gonna do is I am actually going to just remove this particular component from the tank here or from the camera, sorry. And what I wanna do is start over basically. So you guys can see how I set all this stuff up. So I'm gonna hit new folder and we're gonna call this cameras, all right? And inside of this folder, I'm gonna create another folder called code and then two more folders, one called scripts and the other called editor. All right, so if you've done any editor scripting, you'll know why. So all editor scripts and if you don't know why, all editor scripts need to go into a folder called editor and the reason why I'm setting up this folder structure this way is because it makes it easy to export out as an a unity package basically, right? So I'm gonna really used to setting up my folder structures as though I am actually making an asset like for the asset store or I try to keep them all componentized, right? Or modular so I can export out certain folders of code and reuse them in other projects basically. So I don't like to have all of my scripts basically underneath one scripts folder. You know, like you see a lot when you're working on one specific type of unity project. I like to keep it all componentized basically. That's the gist of all that. All right, so we're gonna need a couple scripts here. All right, so what I wanna do is create the actual camera script. So what we're gonna do is we're gonna call this new script, I'm gonna call this IP, we'll call this top down, oops, top down camera. And what we could do. And I do like to name my scripts this way because I like to give it a prefix. And then in this case, I'm just using indie pixel, right? Cause it's an indie pixel script. And then top down is basically a description of what the script does. And then the last part is the type. So this is a camera type of script. So I usually follow that convention. I don't stick to it every single time, but that's why I named it as such. All right, and then inside of the editor folder, I need to make another script. And you don't have to do this right off the bat, but I always like to kind of step things in as I go, you know? And we're gonna do the top down camera editor. And again, that's just another one of my naming conventions that I use while I develop. All right, so to get things underway, what we wanna do is just kind of get things stubbed in over here. So I'm gonna select the camera, go back to my scripts folder and drag that onto the main camera there. All right, I don't know why I'm looking at the tank. We should be looking at the camera. It's just cause it's cool. All right, there we go. Now we're back up onto the camera. So let's actually pop this guy open and get a few things set up before we really dive into the math, if you will, of the script or the meat of it also. I'm gonna kill these other scripts that I had open already. All right, so first things first, I always love to put my scripts underneath a namespace. So in this case, what I'm gonna do is put it under a namespace I actually already have in here right now but called IndiePixel.cameras. All right, and the reason why I do this, if you don't do this, I don't see a lot of people do this in their tutorials at least, but I haven't seen people do it obviously for production projects at work and stuff like that. It's a great idea to do this because it basically encapsulates your code into a namespace and it protects it from clashing class names or variables or method names from like asset packs that you download from the asset store. By doing this, you're insured that you will never get one of those errors that says, oh, this class already exists or this method already exists because you imported some asset from the asset store or you imported your buddy's Unity package and you guys decided to use the same names for something, it happens. So the next thing that we wanna do here is I always like to set up my regions. So you don't have to do this, but I'm just so used to doing this now that I should actually just make a template for this instead of auto develop. I do like to think about what I'm gonna be doing while I'm setting this up, but there we go. And one thing that I like to do, I put all my other custom methods in a region called helper methods. Just helps me keep my code nice and clean and organized. It's really the only reason why I'm doing this. All right, so what are the things that we are going to need for this? Well, we're first gonna need a target. So I know I need a target, right? So I'm gonna say public transform and target because we want this camera to follow something. And we want it to look at something, right? So we need a target for that. We need to transform so that we can access the position variable of the transform, all right? And then we're gonna wanna set up a height, right? So we want a public float M height. And I like to use the M underscore, so the camel casing, right? Or the, sorry, excuse me, the Hungarian casing, whatever they call it. I like to use that because then I know anything with the M underscore belongs to the script and it's a member, basically that's what it stands for. It means it's a member of this particular component. I mean, there's so much discussion about naming conventions out there. This is just one thing that I do, right? I've gotten used to. So I wanted to explain myself a little bit there. And I should actually initialize these with some values that I found to work well when you first add this script to a game object. We don't wanna flow, watt or whatever that is. All right, and then we want the angle because I wanna be able to set this at like a 45 degree angle to my object that I'm looking at, my target. All right, so there we go. Perfect, that is pretty much all the setup. All right, so basically what's gonna happen here is we're gonna roll into the start method and the update method and we're gonna update the camera itself. So I like to move all that stuff into a virtual method because I might wanna override it with some other custom functionality. So I might write another class that just extends this particular class itself. All right, so this would be considered the base class and I could use a protected virtual void handle camera type of method. And inside of here will be the meat of the camera. And then what we could do is we can just say on the base class we're just gonna update or we're just gonna handle the camera, right? And that way I never, in all the scripts that I write afterwards that extend this particular class, I won't ever have to write the starter update function again. I just literally have a class with this functionality that overrides this particular method. Now, if you're not familiar with virtual methods and inheritance that might seem a little confusing to you at first, but I'll do my best to explain it as I get more videos out on YouTube and walk you guys through the way that I like to code up my components inside of Unity, all right? So that's pretty good for this video. I think in the next video we are gonna go through and actually type out the meat of the code that actually moves the camera into position and follows, in this case, the tank. So let's actually update and make sure we didn't get any errors. And if we're all good to go, which we are, then let's actually call it good. I'm gonna assign the tank just so I don't forget, right? And that's another reason why I wanna set up a menu, a custom menu up here. So that way when you have a tank or anything selected, you could say I wanna add a top-down camera script to that and it'll auto-populate the target field right there. All right, so now that we got our script all stubbed in, let's move on to the next step and get this puppy actually following the tank. Thanks so much.