GMS2 Cameras: As Simple as Possible

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey game makers pixelated Pope here and let's talk about cameras the camera system in GM s 2 is the cause of a lot of confusion the goal of this video is to simplify cameras for you if your project is duty if your project does not use split-screen in any way shape or form and has only one view if you are not using split-screen and you have more than one view tell me about it in the comments because I'd like to explain to you why you're wrong so before we get into that let's talk about what you should not be doing when you're using cameras in GM s - first don't use the room editor to set your views up don't set your port size your your view size any of that object following is obviously right out so don't do that second don't use any of the variables with the word pork in them these variables are only usable when you're doing split-screen stuff they really shouldn't be messed with otherwise I've yet to find a good reason to unless split-screen is involved and you need to set up the positions of where your views are being displayed within your game window third don't create cameras you don't need to there's a default camera in every single room that automatically gets created and cleaned up and everything and you don't ever need to create cameras you just use the default one and I'll show you how to do that with that out of the way let's just jump right into this let me show you what we have to begin with I've already created a room with a camera a player and it's super tiny but you know there's nothing else going on here I've just got a player who's going to represent what we want our camera to follow in our room so object camera is completely empty right now let's set up a few things in the create event where you need to decide on our ideal resolution I go over this a lot in my resolution tutorial series which I know a lot of you have already seen if you really want to do this hardcore for dynamic resolution scaling and pixel perfect scaling that's where you want to go look all of that can still be applied to this solution I'm just gonna keep things simple and say divide 1920 by 1080 by 6 that's gonna be my view size I recommend you do this if you don't know what resolution to choose higher this number the better you'll scale the more resolutions perfectly and the higher you need to multiply the scale the less noticeable distortion will be so this is a good way to start alright so now let's set our window scale I've chosen to scale my window to 3 times the size of my view this will make it bigger on the monitor and so I can actually see what's going on and won't be super tiny so let's go ahead and set that window size so I've sent my window size to the view size times the window scale and now I need to actually Center the view on the monitor I use alarm 0 equals 1 it has to happen one step after you set the window size otherwise the new size will not be taken into account when you call windows Center so over here in alarm 0 we just call window center next thing we wanna do is resize our application surface the application service will control how much of the pixels in our window we're actually using the higher the number up to the size of the of the window the more resolution you have to work with and the smoother things can scroll but for purists who are building a purely retro game that should look like an old NES game or an old Super Nintendo game you'll be breaking your pixel grid and I personally don't think that's a big deal but if you do you got two options basically you want to resize your application surface to either the size of the view or the size of the window we're gonna do the window and that is it for a create event again if you want to get more complicated do dynamically sized views to aspect ratio of your monitor that's fine just follow what I talk about in my resolution and aspect ratio tutorial and apply it here you'll be fine so the next step is we need to enable views in the room again the goal one of the goals here is we don't ever want to edit anything about our views in the room editor so with that in mind we've made our camera object persistent and we are going to add a room start event so that every time we go into a new room start our views are automatically enabled so to do that pretty easy you got turn on views user enable and then you got to set your view visible since again this is for somebody who is only using one view and isn't using split screen we just are going to use view 0 so now you visible 0 equals true perfect and that's it now whenever we start a new room the camera will automatically enable these views and they'll be visible but we aren't actually using the view yet it's there it's working but we're not manipulating it ourselves that we are going to do in the end step event the reason we choose the end step event is we assume that in the step event is when you'll be moving the object that you want to base your camera's position on so the players X&Y position is being updated in a step event and once it's finished moving we can rely on this end step event taking place after that you always want to position the camera after all movement has been completed so we're gonna do two things in the end step event we're going to set the size of our view and we're going to set the position of our view in that order that order is actually pretty important so let's start with the sides but first there's something we gotta do because it'll save us a lot of typing let's create a macro macro view view camera zero we're going to be using view camera a lot so let's just make it easier to type we're just gonna call it view so let's set the size of our view and that's pretty much it if you have zooming where your view changes size to either zoom in on something or zoom out to show more you'd want to set that up here and make sure you do that before you position the view so let's talk about how we're going to position it we need to Center the view on the player let's explain how to Center our camera on something first off so this trips up a lot of beginners so let's talk about how you Center your camera on something so let's pretend we have a room and in that room you have a view and that's our view and of course we have the player which we want to follow and I use a green dot so the first thing you do is remember whenever you're doing you know maths in game maker you're especially with rectangles you're almost always talking about the top-left corner right in fact I'm gonna go ahead and say you're always talking about the top-left corner so when we take the view and we put it on player X player why we're essentially doing this so that's not centered obviously you know that's gonna put the player in the top left corner of the screen so how do we get it to be centered well the view we know its width and we know its height and we know if we want it to be like right here then we're gonna have to know the half of the width and the half of the height and then all we do is we take the views position and we slide it halfway so we subtract half the width and then we subtract half the height and now it's centered in the view we know that the player exists let's take the players x position and y position half the width half the height and subtract it from players position and that's where our view is going to be placed our x position for review is our player's position - half the view width and our players Y position - its view height that's where we want to set the view so to do that all we do is camera set view position again we're gonna pass it our view which is you camera zero and then our X and our Y and let's run that and see what it looks like all right there we go got a nice sixteen by nine view and window and there you go so it's following him perfectly but there's a couple problems the view can he's always centered right so we want to make sure that in this case the view actually stays locked to the inside of the room and it looks a little boring doesn't it it's just like right on him and it always is rightness in the center perfectly so let's fix that so the first thing we need to do is clamp our positions we want the X to always be in the room but we were just talking about how we are always when we're doing rectangle math and game maker we're always talking about the top left corner so we need the top left corner to not just stay in the room but to stay in the room with enough room on the right so that the view doesn't move out of the room let's go back to our drawing to explain this so if we've got a view and we're basing everything on the top-left corner here's our view and we don't want it to go outside of the room so how do we do that well if everything is based on the top-left corner if it's in the top-left corner here then the position of the view is zero zero so we never want it to be a negative number simple enough but if we were to say x and y can't be greater than room with room height and that's gonna put it right here so it'll go out of the room to the right and down we don't want that but it seems pretty obvious what we do need to do is subtract the width of the view and the height of the view and now it'll stay perfectly in the room so let's do that using clamp so our x value is now clamp object player X view with / - zero because we don't want it to be less than zero ever and we don't ever want it to be less than or greater than room with - view it and we do the same thing for our Y except we're using our height values and that will lock the view to the room let's add a little bit of smoothing to the camera just to make it just to give it a little bit of character to do that we need the views current position and we get it by using camera get view X&Y and now you've probably seen this a bunch of other tutorials so we're just gonna use lerp we need a value for how fast our camera is gonna follow I'm just gonna call that speed and say point 1 the higher that number the faster your camera will move we move our camera set view position down here all right so we're setting our cameras position to the interpolated value between our current X and our target X by 10% so let's see what that looks like and there you have it the camera follows our character stays in the room and as it approaches the character's position it kind of slows down which is pretty cool looks pretty nice and that's it GMs two cameras as simple as possible hopefully you found this video useful if you have any questions or comments be sure to leave them below thanks to my wonderful patrons for helping me keep my channel ad free and thanks for watching now go make something awesome [Music]
Info
Channel: PixelatedPope
Views: 43,076
Rating: undefined out of 5
Keywords: Game Maker Studio, YoYo, GML, Tutorial, Learn Gamemaker, GMS2, Cameras, Views, Pixelated Pope
Id: _g1LQ6aIJFk
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Wed Dec 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.