GameMaker Studio 2: Complete Platformer Tutorial (Part 8: Screenshake)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody in this part we're gonna be looking at a classic topic I have covered several times before but covering in a slightly different way for this series and that screen shake so now every time whenever I shoot the gun screen shakes and when we kill an enemy it shakes even more and we're going to set up a nice flexible system that modifies the position of our camera by a little random amounts whenever we want a screen shake and we'll write a script then we can call just whenever we want to actually makes a shake happen in any circumstance we can just write screen shake and then a couple of numbers for how long how big it is and we get a cool screen shake okay so the first thing I want you to do is open up the camera object and go to the create event which is gonna set up some variables in here that's gonna deal with our screen shake and I'm gonna be the numbers that will change around to actually set a shrink happening at any point in time okay so I want to start shake underscore length this is what we're gonna set the how long we want a screen shake to happen we're gonna set it with a script later but I'm gonna put some default values and these for now just so that we can see a shake happen at the start of the game I'm gonna set to 60 so it's me 60 frames so like one second worth of shaking okay shake underscore magnitude that's gonna be like how strong the shake is in any direction so how this works is we're basically gonna randomize the camera position a bit and offset it by a random amount in different directions up to a maximum of whatever I shake magnitude is okay so obviously with a higher number it'll shake more violently and with a lower number at all it'll it'll shake smaller okay just so we can definitely see it off the bat leave it to six which will be set up to six pixels in each direction like we can go up to six pixels to the right of where we are or six pixels to the left it could be a little six people down kind of thing okay so there's a kind of a radius of six if you will of how the screen can kind of shake around the last variable here a shake remain okay and that's gonna be the same as all magnitude and we're gonna use it for the same thing but while magnitude is gonna be what the shake starts at and what its maximum is this is gonna be how much we have left and will decrease this value until it hits zero and then when that is zero we won't shake anymore and then when we say a new shake up it'll remain a magnitude back the big number and it'll work its way down you'll see why we have to have these two as different as we go on only one more variable I want to set up in here and I'm gonna type the buff equal on thirty-two buff short for buffer but I didn't want to write buffer because buffers are a natural like thing in game maker and even though buffer itself isn't a specific constant um since there's a lot of stuff involving that it might be a constant one day for all I know so I'm just buff because I just I literally mean it in the sense that we're gonna add a bit of a buffer to our camera position to make sure that we don't shake the screen outside of the game room again you'll see what I mean as we get into it those are the variables in even our and those are the numbers and one you just certainty okay so now let's pop over to the step event where we actually update the camera in the camera position what we want to do is use those numbers that we've just set up now to apply a shaking effect to the camera so this is where were your day all a position make it follow the object around the room and everything like that after we've done all that and yeah after we've actually done our clamping that keeps the camera inside the room which is something we'll come back till make a little edit to in a minute what we're going to do is basically just randomize the X&Y position a little bit so I'm going to take our x coordinate and add to it using plus equals x plus equals random range okay and there's going to be a random number between negative shake remain and positive shake remain okay so say that shake remain is six which it is start here it's gonna get a random value between six and positive 6 okay which will obviously offset our exposition around a bit horizontally by six and then we do the exact same thing for y so it can just copy and paste that line change X to Y white looks good round and range - shake remain positive flavor I mean then all we need to do in fact I should put a little comment on the start here entry screen just so we know what this does we want to decrease our shake remain on every single frame so shape remain equals and we want to decrease it by a certain amount and we don't want to ever go below zero otherwise that'll start gaming there's a weird negative shake okay and so it'll just sort of increase to shake again that and you'll start increasing again once we've gone to zero which we don't want to happen when I just want to hit zero so good take max again max returns the biggest of whatever of all the values that you put into it so zero which is obviously going to be our smallest potential value and once the second value goes below zero we get zero I've done this before I think you know how this works by now so max is zero shake remain - and then open two brackets 1 divided by shank length close 1 bracket multiplied by shake magnitude close bracket close bracket semicolon ok so how this works is by doing one divided by shake length so that's 1 over 60 and multiplying that by shake magnitude that gives us one sixtieth of our shake magnitude okay and it like that's our total magnitude okay that's the the highest peak of the shaking okay so by removing one sixtieth of that value from shake Ramin every frame it means that in 60 frames once I guess basically 60 60 sixtieths right shake remain will eventually equal zero right well it'll brought it all the way back down to zero which means that basically that that's what makes our shake length work okay by reducing reducing shake remain by one sixtieth of its initial value every single frame or one over whatever shake length is if it was 120 then this was last but 120 frames or two seconds at 60 frames a second and so on so forth okay and that's really that's all you need to make us look so if I press f5 now and run the game you should see right at the beginning you can see there's a bit of a shake at the beginning that's something that gets caught in the transition but you can see it happening again see it slowly decreases to one over one second okay it takes one full game second for it to come back to zero if I change those values as I say if I change this to like 240 that'd be like four seconds it's just the number of frames divided by 16 you see that lasted way longer and again if I turn to the magnitude up and so on I would also think that back in the step event though what we want to do is we want to modify this section here keep camera Center inside the room because if I run that again you'll notice there was a problem and that we're now shaking and like you can we're starting to see the outer edges of the room of it around here because because the shaking is happening after we clamp after we clamped those they D x and y value of the camera to be like here or wherever right so that means we're seeing this a little bit of blue of this kind of background at the edge which we don't want now I guess one way you could solve this would just be to put to put the screen shake before we clamped the room but then what you'd find is you would lose the screen shake when you're at the screen edges because you would try and shake outside of the screen they get clamped back into the screen and it would just nullify the effect so whenever you are at the edge of the room you wouldn't see a screen shake so we don't want that but we don't want to shake outside the room so what do we do to fix this well given we're using a tile set we can't add tiles on the outer edge of the room here so the only thing what we can really do or the easiest way to solve this is just to add a buffer to how we're keeping the camera inside the room so we keep the camera not just at the very edge but say 32 pixels in to give us a one tile leeway around the edge of the room but we can't ever really see but we'll see a tiny bit of when we shake the screen but that'll be it but just to account for our screen shake okay and you'll want to make them make sure the buffer is at least as big as the biggest screen shake you intend to have because obviously if I set the screen shake to like we're gonna go with 32 but if I set the screen check to be 33 for example then there be a chance of going over the buffer and they get in seeing outside the room again okay all I need to do is basically so we are the moment we're clamping to view the width of the view half and room with - width of the view have for our X right so what I want to do is just add for the first coordinate the minimum of our clamp but which is our variable that just is 32 from the korea vamp and subtract it from the far-right view okay because less is left and more is right okay and again the exact same thing the y-coordinate okay so now you'll find if I run this you'll see our camera is actually clamped a bit closer in so you can see we can't quite see the outer edge of the room or the the outer edge of the room on the left or the bottom here but it also does mean that we are warm picks what one tile the other end will never normally see this outer edge of tiles okay so bear that in mind as you're designing a level you might want to adjust your level a little bit if you've designed around if you've not designed around that limitation but it's important just to give yourself a bit of space to serve the shake looks natural and we don't see extra we don't see outside the balance of the level okay so now we have a screen shake but it happens just at the start of a start of our game right and that's not really very useful to us we wanna be able to just call a screen shake whenever we want okay so let's go back to our tree Evan let's set these variables back to zero okay so shrink remains zero shake magnitude zero and shake lengthen zero okie doke and now what we're going to do is create a script that will set these values whenever we want to have a screen shake okay so right click and scripts and hit create and then I'll call the script screen shake seems obvious enough right so that I can just call the commands which we just type screen shake and it'll turn yellow and I can put in the values after here and do it however I want to wherever I want to you so just as before in the previous partner slide transition I'm gonna set up the autocomplete stuff for this so I'm gonna type desk after three three four slashes at desk screen shake magnitude frames okay those are the two values we want we want to turn at how much it's gonna shake and how how many frames sport and set those arguments so an OGG magnitude sets the strength on the shape this stinks okay oh I guess a radius pixels is kind of the best way of putting it and then three slashes AAG again frames sets the wing shape in frames 60 equal one second 60 FPS so then if I type screen shake open a bracket you can see okay it's not quite a dated itself yet but it will do I guess it's probably I've closed you up in that screen shake open bracket oh I put soggy I was wondering about a second one didn't show up let's try it one more time screen check yeah so you can see here not Scott - argument screen shake magnitude on frame so it'll tell us what to put in when we call this so when we call the script what do we want to happen well I'm gonna do with Oh camera since that's where all these variables are held if you're being extra safe you might want to do a check to see if camera exists the pace of ball game that's always going to exist that's not a problem if argument 0 so if our magnitude is greater than shake remain the reason we do this check first is to make sure that if we are doing a bigger shake than the one that's currently going it overwrites the smaller shake so we emphasize bigger shakes okay otherwise you might find like if you do a really big shake when something explode that you've got a little shake going off when you fire the gun a little shake overrides the big one and then you never see the big one which is obviously give me rubbish this isn't perfect because I guess sometimes maybe you want a really long but weak shake and want to do big shakes on top now and again the system doesn't really support that because we try and do a big small shake it'll just overwrite the little long shake if you want that kind of thing you'll have to make a system a little bit more complicated than this but this is just the basics and it's good enough for everything that we're going to need for this game so this is what we connect with okay so shank underscore magnitude magnitude magnitude equals argument zero okay that's not magnitude here that's all for this document of the script then shake remain again you pull argument zero it's gonna equal the exact same thing shank length equals volume one notice that we base this on shake remain as well still alike it's not based on whether or not the initial maximum magnitude of the the shake is bigger than the shake we want to apply it whether or not the shakes that we're doing currently is bigger so say you've almost worn off so you say you did a shake of six or whatever and now that's degraded all the way down to two and you try and apply another shake of for that one will over I this one but if you try to apply a shake of one it wouldn't it would keep this to one going until the to one finished okay okay and that's really all there is to it so now I can just call that wherever I want there to be a shake so I'm gonna go into the gun object and the begin step where we did our shooting and it's find and here I think it was here with instance create their bullets or bullet this is where we're creating a bullet so this is whenever we shoot I'm just gonna type screen shake we're gonna go with a magnitude of two just to keep it quite quite so well just not too over-the-top since we're gonna be firing looking a lot anyway I'm still going to be quite decent shake so - you'll be good enough and we'll give it ten frames okay because we don't need one don't want any - one to last a really long time so that's like one-sixth of a second okay now if I run the game you say you've got no shake at the start at this time but if I shoot the gun the shake starts happening is going off okay which is already really cool and adds a lot of really cool feedback and just sense of polish and just it just makes the gun feel weightier again again you can keep going with this stuff forever like just with like the recoil and the gun and the screen shake and all the bullets being really big and all that kind of stuff you do that kind of thing forever like with we've spent a long time in game design getting the feel of a shooting a gun right so you'd be a fool not to use all the lessons we've learned along the way and screen shake is definitely one so that already feels really good now let's add another screen shake when an enemy is killed for example so on Oh dad when these get created which is obviously whenever we kill an enemy let's do another screen shake and that's makes one way bigger so it's go with six four twenty five okay so it's a much bigger shake so it should overwrite our small shake when it happens so that we've got a smaller shake here and if I shoot one of these guys we get a bigger shake as he dies you can see about having a signal again so you can identify the moment that each one dies very very clear that sort go okay and that's really all there is to it that's a simple screen shake system for your game and it's really easy to call it just whenever you need to and decide okay what this bigger shake for this long and how ever happened as I said if you've got a game that really relies loads on having loads and different screen shakes and different spirits of different times you want smaller ones and the writing bigger ones and so on you might want to look into something a bit more complex probably something that say creates like a list of these or something and applies basically they can apply more than one screen shake at once because that's that's the limitation of our system here is in the camera in our update we have one set of shake very shake numbers and we just apply them all at once whereas if we wanted multiple different shakes on the go would probably want like an array of different shakes or whatever and then like add those together to work out what our shake should be on any given frame and so on but I think that's probably a bit much for this particular tutorial series I don't think we really need that but I might cover it in a future tutorial it's not linked to this series okay I hope you all enjoyed that hope it's been a bit short at this time not another crazy 50 minute video and I'll catch you guys next time a huge shout out to my patreon supporters without whom these videos literally wouldn't exist they fund a great deal of its work they also play a part in deciding what topics I cover and if you want to be a part of that and you want to be a part of this channel and and its growth maybe consider dropping me a couple of dollars a month on patreon a huge shout-outs in particular two down in a mule Alex Ray Charles go Murray Angel Rodriguez Harold Guidry Nathaniel Walsh Lewis our Pereira Steven Hagen Jason McMillan Oh and Morgan bowels of the dog and John Grimshaw thank you very very much for your continued support see you guys next week
Info
Channel: Shaun Spalding
Views: 97,488
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Game Development, Indie Games, Tutorial Series, Game Maker Studio, Making Games, How to make games, GameMaker Studio 2, GMS, GMS2
Id: r9OYXCUydGg
Channel Id: undefined
Length: 17min 53sec (1073 seconds)
Published: Fri Oct 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.