GameMaker Studio 2: Complete Platformer Tutorial (Part 3: Shooting & Recoil)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to part three in this part we're going to be covering giving our player a gun we're going to give them a gun is going to be able to shoot some stuff and we're going to make it and give it a bit of recoil on the gun but a kickback they go and follow the mouse around and it's going to be very exciting beginning of this is going to be very straight forward it's going pretty simple but then a little bit later in will be learning about some new functions so we don't use before called linkster underscore X and Wednesday underscore y so do keep an eye off that and try and make sure you understand it when it does come up because it will be very very useful to you going forward if you decide to stick around and keep making video games which I hope you do before any of that though first thing we need to do is just bring in a sprite for the weapon we're going to have okay so I'm going to right click and sprites and create we know how to do this by now import I'm going to bring in s gun okay I'm going to make sure from now on as well all the resources I said this before but I think it round to it yeah so from now on when this video goes out all the resources will be in the description of the video for you to get in case you want to follow along and use my exact resources so that you get everything line by line exactly how I've done it you might learn a bit more if you're using your own assets because you'll have to change some numbers around a little bit just to make everything work out perfectly but it's ok you can do whatever you like I'm going to name the sprite s gun okay and then I'm going to position the origin for this gun I'm not going to have it exactly and then I'm going to have it kind of between the two hands and it's hard to see the zoomed in but the idea here is it's like a hand around there and a hand on there which looks a little clearer when it's actually on the player sprite um but yeah about ten ten by four that's where I'm going to hand that just by dragging that crosshair to the right place okay so our next step but as usual is to add an object for the sprite we've just created right so I'm going to right clicking objects and create and call it oh just the same as a sprite just the same as the pattern we've been using for everything else going to set the sprite to match s gun for a gun and then what I'm going to do is I'm going to place it in the room and I'm going to add it to its own layer because we want to make sure it always shows up in front of the player so I'm going to create a new layer call all gun and I'm going to drag a new instances lay them out now noticed along with the little circles and they're just going to drag a gun into the room while we've got that layer selected okay magic guarantees that we get our depth ordering correct because I mean I could put this on the same layer as the player and like a lot of the time it would probably work out you could probably get away with it but you can't rely on two objects on the same layer always being correctly shown okay if you want to you need to if you want it to be consistent and it knows whether or not want to get drawn in from another you have to have them on separate layers okay it's really is the best practice now the reason that I've made the gun as a separate object and we've got our kind of player with no arms um running around is because I kind of want the gun to be able to move and act freely of the player okay so even though it's going to be kind of locked to the player in terms of position well the player is walking around obviously doesn't want to actually flow like this detached from them I want to be able to point it wherever the mouse is and maybe give it a bit of recoil and stuff when we're shooting and things like that and that's that's just a bit easier to accomplish and easier to one easy to think about when you're working with it as a separate object okay it's not the only way you could do those things but it is probably one of the simplest so at the moment if we were to run the game we just get our player which will run around and have all the stuff we had for the previous episode um but the gun would just hop it advocacy and anything with it right so what we're going to do is make the gun follow the player around and you'll see oh so I do this one of these other reasons for making it a separate object is it lets me do some fun stuff so I'll double-click an old gun just bring us to our gun object again and press f12 give us a bit of space here add event and I'm going to add the begin step is I'm okay now we've used the step event before which is an event that just happens every single frame of the game right and the begins that works exactly the same way only begins steps will happen before any steps happen okay so if you need something that needs to happen every frame that needs to happen or any other objects snapper than you can do that using a begin step and you can do the opposite by using the end step and do something after every other step event has taken place that frame okay now I'll explain why we're using this particular step of them in a moment the code I'm going to write in here is simply x equals Oh player dot X and go on and y equals player dot y semicolon okay now I'm going to run the game and kind of demonstrate what this does so you'll notice the gunner's kind of mood is kind of on our head at the moment which is a problem that we will fix but as I move around you might know if something now the code I've written down you probably work it out it's pretty simple it's just setting our X&Y coordinate to match whatever our players is but if you notice that doesn't quite keep up you know this is a slight bit of lag um if you look very closely between where the player is and where the gun is the gun it appears to be and because it is is actually one frame behind the player until you stand still and then it gets a brain to catch up you know but while you're moving around it's always one frame behind and that because we're all dating it in the begin step event and doing this allows you to kind of create a very simple feeling of weight to the weapon okay it feels like it's actually carrying it around like it's got a bit of force to it because he's got to kind of look it around it's not too obvious at the moment because he's kind of carrying it on its face so I'll fix that now no easy way to fix that is to just add a number to the white one let's just bring it down a bit okay and just manipulate this number until you get it in the right spot now you might be thinking well why not just change the origin point for that okay if we look just over our at our going over here we can see our origin isn't in between our two hands here if I would have just raised the origin point up that would achieve the same thing as doing this um but we want to be able to rotate this gun around and do other things based on its origin later that aren't going to work very well if our origin is up here somewhere right we want to keep it between these two hands because we want that to kind of be the center point for the gun in terms of like rotation and like where it's aiming at and so on but let's just see the effect here say plus ten will work from in or work for you if you using my assets you might need to play with the numbers a little bit yourself to get your own asset working but you can see now he's carrying this gun around and because of that just a little bit of lag add it to the gun he just has a tiny bit of weight tiny sensation of kind of like weight or force kind of on this gun like it's like these really holding it and it's sort of like a dynamic sort of addition to your animation in a way and just helps the sprite feel a little bit more alive but to you maybe you don't like the object but I think it helps kind of make and feel a little bit more real okay so the next thing we're going to do is at the moment again just sort of always faces to the right like this and we're going to make it face towards wherever our mouse is so if I close the game now and just still in this begin step event enter well we'll keep doing stuff in the beginning step event for now you might want to swap to the main step event if you want doesn't really makes too much of a difference oh it's just the subtle things like that now and again so I mean it's up to you which event you want to put this in but just the clarity I'm just going to keep most of the stuff in the beginning step event for the sake of this tutorial okay so what I'm going to write in here is image underscore angle you can see that turns green as another property of the object okay just like our X&Y on or other things that we've done with our player objects before the image angle controls the angle at which the image is drawn to the screen or did the image of this object will be drawn okay and by changing it we can rotate the gun and we want to rotate it based on where our Mouse's from the screen okay so it's like point dying wreck okay so than easy in see turn Yeller as a simple function that you give a one corner and another coordinate and it works out what the angle is between those two coordinates okay so I'm going to put in our current coordinate which is X and the coordinate that we want to work out the angle to which is the mouse corner and we can find that any point in the game in any script anywhere if you ever need the mouse corner all you need to type in its mouth from the score X and Mouse underscore Y and that will return the current Mouse position in the room of your game at the current coordinate of your mouse in that room okay and because we've given at least two corners that's all we knew includes the bracket now add the semicolon and now that's going to make sure our gun points towards our mouths okay so if I run the game there I mean see it starts to spin around and it points where our mouse is pointing okay you know this is a bit of an issue that it flips upside down whoever won we're facing to the left either hands appear to be on top which I know some of you might like that kind of approach for later looking like you're kind of holding it over your shoulder type thing um but uh well we will we will look into sort of fix and now making it sort of standardized layer and you can play with it yourself and get a peek sangs look you want for your weapon but we'll look into that eventually oh we've got other things to do first the next thing we're going to do actually just change from these controls around because at the moment you might especially if you're following along you'll find this a bit awkward to control with our current controls I've got the arrow keys to move and space to jump which is fine when you've got both hands on the keyboard but if I want to aim with my mouse I kind of need a hand for the mouse so having to stretch my hand on my keyboard at least from the arrow keys to the spacebar as it's pretty card I'm doing it right now it's pretty difficult so I'm going to just swap the controllers to kind of a wsad type thing very typical kind of gamy controls which I'll show you a bit about how to use arbitrary keyboard buttons as well as just the arrow keys and spacebar and that sort of thing so let's come into our player object and you just open this up and maximize this because there's quite a lot of codes and the other kind of see it all on at the top here where we get key left key right and key jump um you see at the moment we've got keyboard check VK left the UK right are what I'm going to do actually instead of just changing these to be something else I'm going to add two line symbols like this okay for me that's a pressing shift and the backslash key on my keyboard um it might be somewhere else on your keyboard but that's where it is for me but you can also type the word or and that turns yellow and this does the same thing I'm just going to use them to symbol because that's flat what I use to basically and as you might have guessed from what I just typed it means or so I'm going after those three symbols I'm going to write another keyboard check and I'm going to put in instead of VK laughter and like that man right Louis Ord Open bracket open quotation mark and we're going left so we want the a key and close those two pairs of brackets and end with a semicolon so what this has done here is I'm setting key left that variable to equal keyboard left VK left keyboard left keyboard check VK left just as before or keyboard check or a so what or is going to do is it kind of does a binary thing like if this is zero then key that would be zero right and if keyboard check or a is also zero and keep it left will also be zero but if either one of these is one then key left will be one okay so it'll either take this value it is true or this value if it's true if both of them are false then it comes back false and this will be zero okay but if either one of them are one key left we'll get set to one and that's how all works and you can do this multiple times I could keep adding ORS on the end like that and if any one of the conditions is true um we'll end up putting a wand into key left and what this means is we can now use the left arrow key or the a key we can use either one in order to move left so I'm just going to copy this section here and paste it on the end of this line here I'm going to change the a there to be this odd command as you've probably guessed um is just a function that returns the exact key code I guess all over all the keyboard folder the keys on your keyboard have like a number code associated with which sort of button which position they are on the keyboard and what this does is it takes the letter you want and translates it into that keyboard code which we can then use in the keyboard check to get that particular key it's exactly what VK left is actually doing it's just providing the code on the keyboard for left arrow key it just happens to be a built-in constant whereas there are the curly letters we need to work it out by using Ord okay and just putting the capital letter that you want to use okay so if I run that now we'll find I mean you won't see anything different on the screen but if you trust me it's much easier to control now I can use the a and D keys and I can leave the spacebar the same because I can reach that very easily with one hand use whatever controls you like but there's a way of being able to use multiple different control schemes for different people at some point in the future we might look at rebinding keys although that'll probably be a separate tutorial from this series and we also might look into gamepad controls at there later on as well but for now we'll leave these as our current kind of controls so we have a gun now and it follows the mouse cursor around the next thing we want to do is actually add our bullet okay we want to add something the gun can actually shoot so I'm going to right click and sprites again hit create import and I'm going to bring in S Plus and score strip to buy them in as a strip to it splits it into the two frames I want now you might be wondering why we've got these two different frames we've got a big circle and then something that looks a bit more like a bullet um what I'm actually going to do is a pretty simple trick I'll just go call this mess alright I'm a pretty simple trick for creating a muzzle flash on a gun okay or any-any like a weapon that you can shoot right because you know you've all seen that in game to shoot a guns big flash of the style of gun kind of create a good piece of visual feedback and a very easy way to do that is to just make the first frame of your bullet a big white circle of thing that looks like a big muzzle flash and then what all we need to do is when the bullet finishes its animation we just tell it not to animate anymore and then it stays in Internet in this frame so then the bullet travels like that a very very simple way of creating a muzzle flash which you'll see in effect in a moment if that didn't make sense to you so where we've got speed at the top here um do make sure you set this to 60 okay because that's the that's the frame rate of our current game unless I think it is let me check yep 60 frames second we did do that so you need to make sure this is 60 because we only want to show this for one frame okay we don't want to show some multiple frames otherwise the muzzle flash will appear to sort of travel with the bullet we just want to kind of show at the start the edge of the barrel of the gun just as we're firing okay which is where the bullets kind of going to come from all right so that's our bullet sprite on and now let's move on to making our bullet object so right-click an object now hit create its course but set our sprite to be s bullet and then what we want to do is actually create another layer in our room so let's come back to our room editor arm because again we're or don't want to keep keep making sure we have a fine control over what's shown over what and what's shown under one so it's create another bullet layer so let's create another instance layer and we're going to call it bullet hole at voice I guess because it's going to be more than one of them probably quite a lot of the time and we're going to make sure that that's just above our gun and above our player okay so it just shows up above everything so we get a nice big circle on the end of that on the other muzzle flash so it doesn't go under the gunman and so on okay once we have to place anything in the room because obviously we're going to create our bullets dynamically okay so with the others we've just placed them in the room that's one way to make an object in your game but you can create them dynamically at runtime which is what we're going to do next um well I feel okay we're going to do a few things to the bullet object first but then we're going to do that hey first of all though we want to open up bullet object and we've got a few things we want to sell in here before we do anything else there's not a whole lot we need to do to the bullet we just need to settle some things just to say there's a bit of time doing it later on just to make sure the bullet behaves how we want it to most of the stuff we want to do with the bullet is going to happen in the gun object but we do need to set up a couple of little things here the first thing I'm going to do is go to add event and I'm going to add the other animation tend it done okay so other animation end so this event happens as you might have guessed when the animation comes to an end so at the moment by default we've got our bullet and it will animate 60 fps and then it will finish and then we can do something when that animation ends and what we want to do is make sure the animation doesn't happen again we can simply do that by typing image speed equals zero okay now we'll multiply our current animation speed of 60 frames a second by zero leaving us with the speed of zero so it won't animate any more and then what we want to do though because our animation will have finished it will have looped back to our first frame which is the muzzle flash but that's not what we want we want to fix our image on the first frame of our image not the zeroth frame okay we are on frame frame one so it's the second frame but it's frame one I know it's kind of confusing I can go went over this before but this is frame zero and this is frame one so we want to leave it on frame one we knew that just by typing image we've got index into is one okay I'm just some of the similar stuff to what we did with the player sprite and animation just changing those values and that will make sure that we are on the circle frame for one frame of our game as 60 frames a second so just get a very very quick flash and then it changes to this bullet sprite for the rest of its life okay the next thing we want to do is make sure that when this bullet collides with a wall it gets destroyed now the simplest way to do this would be to go to add event and the collision event with a wall and just blow up the bullet oh but we're going to do it slightly more complex way um just to kind of make things look a little nicer I'm actually going to do is use the post draw event so go to draw you see all these various different drawer events um who won't be using peace for a little bit of time yet for the most part for their intended function anyway but we're going to use the post draw function the reason I'm using the post draw function um is it's a very similar event to the staff event and similar to the begin step event that sort of changed the order of when things happen and this does something very similar the draw events are quite like Stefan and that they happen every single frame of the game but they typically have to do with rendering and drawing stuff to the screen in your main draw event that's where your image is actually aware sprite is actually drawn to the screen and that's where everything involved in drawing the sprite and animating it is actually done by the game engine okay by doing something in post draw that's an event that happens for every object in the game every frame of the game so just like the step event but it happens after all of our drawing and all of our rendering is finished okay um what I'm going to do is I'm going to type in here if place anything text why Oh war okay as you'll remember from our collision stuff and our player that's checking to see whether or not there's a collision between this object at these coordinates with this particular object type and if there is we do whatever comes next and I'm not going to open curly brackets for this I'm just going to type one simple line instance destroy and just get rid of it now you might be thinking well why didn't I just do this in the staff of then why do I need to make sure this happens after everything has been drawn well by doing this it makes sure that we draw our bullet before we actually get rid of it okay otherwise you're not actually getting rid of the bullet on you you'll see the bullet disappear before it actually hits its target which you might want you might want it to look like that perfectly normal thing to do it's like that in a lot of different games but by doing this it means if directly down at the floor you'll get to see the muzzle flash still you'll still see like that first frame of animation because it'll get drawn before we actually find the collision with the floor and then Y a little bit okay so we get to see exactly where the bullet collided by doing this which is just makes things a little bit clearer okay you might want to be careful with always doing the sort of poster or poster you usually want to use the draw events just to do drawing stuff rather than logic but this is just kind of one exception that kind of makes things a little bit simpler it's just a simple place to put this and just really cost you anything okay um but we need to actually make it so our bullet shoots so we can actually see the result of any of this but I'll come back to this so that you can kind of see you can kind of see what's going on with this and we'll I'll show you what happens if you would have just use a stack of them so don't worry too much if my explanation of that didn't make too much sense yet so sorry but there was a lot of explanation for just like three lines of code but let's come back to our gun object now it should come back to here and I'll begin step again I'm just going to kind of do everything and I'm going look begin steps because why not really I could move this stuff loose at events I don't really need to so I'm not going to yet okay what I'm going to do is going to check to see if we're pressing the mouse button and if we are we're going to shoot um actually though before I do that I need to add the create event or I need to do is add a variable in here quit firing delay equal to zero okay OOP excellently pasted about code in the fire and delay equal to you see that x blue would create another variable we've done this before okay we just create a word that represents a number that word firing delay represents of number zero and we're going to use that variable to make it so that we don't fire I'll go in every single frame but the mouse button is held down okay and we just fire it like every few frames while it's held down okay so I'm going to type it now check button MB slack okay and type to ampersand symbols for you can also write and if you want this is very similar to war so this allows us to check I may have covered this before I'm not sure but this allows us to check whether two conditions are true rather than only checking one condition it's different to or and that it needs both conditions to be the same so they can either both be zero or they can both be one or they can both be false or they can both be true they have to match though they have to be the same thing in order for whatever we put after the if statement to happen okay I'm going to use the two lampstands and bulls cuz that's what I'm familiar with again you can just use an if you want in place but what I'm going to put with this and is another condition which is fire and delay less than zero okay and I'm going to open a couple of squiggly brackets here and this is where we're going to foster to actually shoot but just above this statement I am going to write firing till a equals firing delay minus one okay so what that's going to do is it's just going to take this variable reduce it by one every single frame on so eventually it'll go into zero and what we'll do every time we fire a bullet is we'll set firing delay to be like five okay meaning that the next time we'll be able to shoot will be five frames after our last bullet was created okay and I just kind of gives us a five frame delay in between each shot so we'll do that right Gale just like firing delay equals fine okay so it only happens when we actually make the shot which we can only do in it's below zero and then it reduces by one every single frame okay so now I'm going to introduce another new function here tied with okay what with those is a function has okay you provide um the name of another instance or another object so I can do like a player or something for example it's not what we're going to do but for example and then open another pair of squiggly brackets and what this would do is instead of applying whatever code we write in here to our gun object it would apply it to whatever object or whatever instance of an object we place inside the brackets so if I did anything in here like X equals x plus 5 or something that wouldn't happen to the gun even though we're in the guns begin step it would happen to the player okay aren't very very useful with statement very very useful what I'm going to put in here is actually another function which creates a new instance and their type instance underscore create underscore layer ok this creates an instance of another object you might have heard me say before I'm not sure if I covered it in this specific series but your objects are like the blueprints of your code ok um it's easy sort of forwards a mistake of thinking but the things running around inside your game are your objects but they're not there are actually instances of your objects that copies if you will of the blueprint that you've established in the game so our player in the actual game world is an instance of a player and so we can create new objects so you can create new instances in the game of other objects by using commands like instance underscore create the sky layer I'm going to open this I'm going to give it the corn that's where we want to make the instance which is wherever our gun is so x and y and we want to give it the layer that we want to create the object on and that's going to be for it ok make sure you add the quotation marks that's quite important um and then the name of the object type that we're going to create ok and we want to create an old bullet so after doing all of that why on earth if I stuck this inside a with statement well by putting this in a with statement what instance create layer actually does and you don't need it to do this every time but when you run this function it actually produces a result so I could type like the name of a variable like oh let's equal instance create layer or something like that right and that function would actually put a value inside the word bullet you don't have to do it like that you can just do the function on its own but by doing it in this sort of way it puts a value into this word statement and what that value is is the instance ID of the instance you've just created so we're going to make a bullet and we get its ie D which allows us to then do stuff with it using the with statement okay it's kind of a two-for-one allows us to create the object and then provide its ID to the width statement to then do stuff immediately to that instance okay what we want to do that instance is we want to make it move um we're going to give it a speed so we're going to type speed and you see that turns green at the built in value equals 25 25 pixels per frame now you might be wondering well if speed exists and you can just get something of speed as a property why on earth did we do all this weird movement stuff in the player well speed once you set it is fixed and then that object will just keep moving at speed until you change the speed it's a lot more manageable when you're doing complex movement like with our player like moving left and right and jumping and all that kind of stuff to manage everything manually by like controlling our X&Y coordinates but if our bullet is really simple like it's just an object that we want to just fly off in a direction and we don't need to really do anything complex or respond to input in any way or really do anything complex so I can just set a speed and it will just move at that speed and then I also can just give it a direction to move at that speed in and it'll it'll just do its own thing and basically kind of manage itself which makes things a lot easier for us so I'm also going to type direction as I just said in that turns green represents the direction um to actually apply our speed in I'm going to type further dot image annual now the word odor has a lot of different uses and in this particular context inside of a with statement if you use the word buzzer which turns red and then a dot you're referring to the property of the original object so what you're referring to gun because you remember inside with anything we do here is apply to this bullet we're creating okay and not to the gun object but we can refer back to things from the gun object by using other dot okay then by typing image angle we're getting the image angle of the gun which is what we set here okay so this is what's going to make our bullet move at the same direction as our gun go make sure we actually shoot right way but that only affects the direction of movement it doesn't actually affect the the actual angle the bullet is going to be drawn out okay we need to change that as well which is which is our image angle right then they type image underscore angle equals direction since we just set our direction correctly we can just name copy our direction I could have written other dot image angle again so you'll see a little bit later wide and do that all just helped us out a bit later okay um that's really all there is to interms of just shooting and we've already got bullets set up to collide with walls and stuff like that so I should now hopefully I've not forgotten anything and then run the game I can move around with guard gun aims where we want it to and if I click you can see we actually shooting you mean you've got that muzzle flash at the start a little white circle just appears right on the peak of the gun luckily the numbers I've used the speed that I've used in everything means that the muzzles like it's actually just worked out perfectly and the muzzle flashes right at the edge of that barrel which is exactly what we want um but you might remember the origin of our gun as hot as hot I like going move around is like between our two hands and that's actually where the bullet is when it's being created but because we said it's speed right away it moves 25 pixels before before before it even gets drawn for the first time okay so that means the first time we actually see it on screen it's actually just happen to move the right amount that's not the best way to manage it it just happened to work out very well in this scenario you might find with your own assets doesn't really work out that way but when we get to a little bit later in the video I know this one's getting quite long sorry about that but when we get to adding the recoil to the gun you'll see um using links to some wonder why how you you would be able to manipulate the position of that bullet if you needed to okay okay so we've got our gun got a bullet I guess in a way we could end this tutorial here you've got mostly everything but why I'm going to do is add just a couple of little nice touches here that'll just gives us a little bit of polish make it feel a little bit better a little bit sexier okay so this thing I'm actually going to do is I'm going to kind of randomize the direction we chew our bullets in just a little bit just to kind of give it a bit more feeling of being kind of a gun you know I'm moving machine gun that's a bit unwieldy and hard to hard to aim right so instead of ending that line there with Direction equals the dot image angle I'm going to type O that image angle plus random underscore range now this yellow function what it does is it takes two values and it returns a value in between them somewhere at random and including the values you give so I'm going to use minus three and three okay so that's going to this function here is going to equal a number between minus three and three inclusive with those numbers okay it could be anything from minus two it could be one it could be three it could be minus 1 etc okay and what that does is we're just going to add that value on to our image angle just to kind of offset the direction a little bit randomly with each bullet with fire now if I run the game and I shoot you see without moving the mouse our bullets have got a bit of a random spread to them it's so but it is there and it just kind of gives us a bit more of a kind of a randomized machines and effect the next thing and this is kind of a big one for me is like firing this gun feels a bit weightless at the moment and we know no we added this kind of feeling when you're carrying it around when you're carrying it and not shooting it feels kind of nice and weighty but it's kind of hard to believe the shooting at the moment because there's no recoil like the gun just sort of hovers there little is just sort of come out like magic and it looks like these bullets don't really have much weight to them looks like must be pretty light not very time enduring or anything like that because like they don't seem to weigh anything might as well just be like a little BB gun or something right so let's give it a bit more weight let's make it feel a bit more give it a bit morton's and we can do that just by giving the gun a bit of a kickback okay and we'll just make the gun sort of come back as if it's kind of a bit hard to control for the person shooting it so how on earth do we do any of this well the first thing I'm going to do is come into our create event I'm going to add a new variable called recoil okay I set out to 0 and what recoil is going to do we're going to set it to say for whenever we're whenever we shoot ok whenever a new bullet is made I'm going to set it to a little number and that's going to represent how far we want to move on gun backwards and then every single frame we're going to slowly decrease it so while we're not firing it'll just return to default position because when recoil eventually hits zero again our gun won't be moving by any amount okay so every time I shoot goes to four and then slowly decrease as well it'll be quite quick really but every single frame decreases by one and gets back to zero okay so we get kind of jittering effect while we're shooting gives us a bit of a kick back so how do we do that well the first thing I'm going to do is make it so when we fire we set recoil to four so and here I'm going to type recoil equals four make sure you do this where we set our firing delay because we're doing it in very much the same way don't set it in the with areas that's too purflex the bullet we want to affect the gun so make sure you do that there and just up here as well when we do our firing delay stuff or when we reduce that by one I'm going to basically decrease our recoil by one every single frame okay recoil equals max zero recoil minus one now you might be thinking well you didn't do the same thing there you use this width and cause max what does that do well what max does is it returns the biggest of the values that you give it you can give any number of values I'm just separated by a comma and I've given it to I've given it the number zero and I've also given it recoil minus one okay so as you can imagine if recoil minus one happens to be bigger than zero we return that and we've done exactly the same thing as we've done up here just reduce the value by one but let's say that recoil is zero when we do recoil - one that will give us minus 1 and zero is bigger than minus 1 therefore zero will be returned all it does effectively is make sure we don't go below zero okay cancers so while recoil is 4 or 3 or 2 or 1 it'll reduce by 1 but when it hits zero it'll just stay at zero until we fire again and it gets sent back to 4 okay very very simple so we've got the value doing exactly what we want now we just need to apply it to our weapon what we're going to do this might seem kind of impossible if you think about it at the moment because we've got our x coordinate we've got our y coordinate right and we can manipulate those in order to move us all around but how do we move just in a given direction well we're going to use what's called length the X okay this function I'm not going to use it exactly like this how it typed it but on this is the function we're going to use let me see if I can explain now hopefully it with some diagrams and stuff how this works okay so we have an X and a y-coordinate where our instance is and we want to move in a particular direction now we don't know the problem here is that we don't know the exact x and y that we want to end up in if we did we could just set our x and y to be that right but we can't do that because we don't know what those are we only know the angle that we want to move that and the amount of pixels legally in that direction okay but we want to move um we can't just use that number of pixels in our X&Y because we might be moving a different amount in X - the amount we're moving in Y right we'd only be able to move at 45 degree implements increments that way okay but we want in the world move anywhere 360 degrees in any direction at any kind of length now if you know a bit of trigonometry you can actually work the stuff out very easily without using this function but what lengths to X and what links do y do is if you imagine that position that we have you know the angle that we want to move and we know the distance that we want to move what links the X will do is given that direction and given that length it will return the number of pixels you have to move any x axis to reach that position and similarly length the X returns a number because YouTube even the y axis to reach that position and once you combine those two results together boom you have the coordinate that you want you want to move to okay hopefully that explanation made sense um hopefully my diagram is going to have made it a bit clearer very very very useful to understand that allows you to move any instance in basically any direction by any arbitrary amount very very very easily if you can understand that um so that's what we're going to do now so we're going to type x equals x minus length the x open bracket and then you can see at the bottom we need to provide the links and the direction just as I said given a given angle and a given amount that we want to move we can work out that X component okay so the length the distance that we want to move is recoil okay it's when this value is falling we shoe and it decreases over time towards zero we want to move a bi recoil amount and the direction is image angle now you might be thinking wait don't we want to move to the opposite of our image angle the image angle is the direction the gun is currently facing don't we want to move the opposite of that and yes we do which is why we're subtracting this value okay we're subtracting it so that we move in the opposite direction just a cheap way of doing that I could have added the value and used image angle plus 180 but I think this looks a bit cleaner than doing that um and that's all there is it took that I'm just going to copy and paste that line and change my X's to Y's simple as that just change the X as the Y's in that line and then we get our Y component and we do the exact thing okay so let me run the game now hopefully I haven't forgotten anything again if I run around and I start shooting you can see now the gun has a proper kick back into it just just bank with every shot and then just returns to the default position when we actually stop shooting okay just can give that gun third of weight bit more on this feels a bit more real like he's actually holding a powerful weapon okay there when he's shooting one thing to point out that I kind of forgot about earlier is you see when we shoot down we still get our muzzle flash appearing but we don't see any bullets that's because we actually set that bullet to only get destroyed in the post roar event rather than in a step event or in a collision event okay if we destroyed it in a collision event or in a step then we would never see the muzzle flash and we just get a sort of jittering thing looking out we're just shaking the gun when we're shooting to the floor okay and we wouldn't get kind of the impression that where you're sort of shooting the floor of it okay I just preferred do it that way and we can also see the bullets collect exactly where they get destroyed on contact with the wall rather than if we did it a different way they would sort of disappear instantly before they hit the wall so they'd probably sort be disappearing kind of around this sort of area um before the frame where they actually makes that collision so we've covered a lot of stuff in this part my voice is starting to strain so we're going to cover one more thing I'm just kind of round off the stuff to do with the gun and the bullet I think we've got a lot a lot of cool work I think it looks quite polished and good so hopefully it was worth it after they were probably being really really long the last thing we're going to do is make it so that our gun faces kind of the right direction you'll notice at the moment when we saw turnout go and we point it to the left our hands are on top of the gun which looks kind of silly to me so I'm going to show you how they can kind of manipulate that and you know you might want to change it so if it's you're in personal taste so this is how I'm going to do it so I'm going to type this just at the bottom of the gun here image underscore angle is greater than 90 and okay and you remember premier li we're going to check another condition and if that both true or that both I don't know just if they're both true sorry they both come back through then we're going to do the thing so if Jambo is great over 90 and if image angle is less than 270 so um I explained to you previously how when you set up a sprite you will kind of want to make it face the right initially okay facing towards the right um especially when you do any we're going to do any angle stuff with it and that's because um image angle is 0 typically represents facing to the right okay an image angle is 90 it's facing straight up an image angle of 180 is facing 180 degrees either way facing left 270 is down and 360 or 0 again is again facing to the right okay so because that's how game maker handles degrees and angles and direction that's why it makes sense just to keep all your sprites consistent have them all initially drawn facing to the right before you kind of manipulate their angles and stuff okay so here and I'll bring up that image again like if image angle is greater than 90 and it's less than 270 that means um we're facing somewhere to the left or gay even if it's only by one degree we're facing the left and if that's not true then we're facing to the right okay so if we're facing to the left I'm going to do them in fact image on Y scale so our vertical scaling equals minus minus three minus one okay and that's going to flip or going upside down oh but as you remember because when we rotate it around around at origin it is upside down when it faces to the left so by flipping it upside down upside down we get it right way up okay and then else so if we're not facing the left wheel with we're not facing the left then we know that we're facing to the right right so if we're facing to the right we set our image by scale equal one okay so we set it back which is what its default is so if I run the game now just off of that you can see the so at the moment hanging Z are going here facing the right just looks as it always did but if I go over the top you can see it flips around and now it still looks like we've got our hands underneath there while holding it properly which looks kind of cool looks like it's going to just flipping it around like that I like it that way you might find your own way that you kind of prefer to do it but there's some simple logic to help you learn how to kind of manipulate how it looks when you're facing each particular direction okay that's all there is for this part thank you very very much for watching I hope you got a lot out of it sorry it was probably quite long and had quite a lot and you stuff going on there but I like to think it was jam-packed with quality learning material so I hope you guys enjoying this I'll see you guys on part 4 which will be not next Friday but on Friday afterwards so I'll see you guys then thank you once again to all my patreon supporters who make this work possible special times in particular - in a mule Charles Montgomery rock song Harold Guidry down Jason Macmillan Angel Rodriguez and Owen Morgan thank you ever so much for your continued support I'll see you guys in two weeks for platformer series and next Friday for I don't know my patreon supporters are currently voting on what that video is going to be so we'll see what it is next week can't wait see you guys then thanks for watching
Info
Channel: Shaun Spalding
Views: 338,826
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, Platformer, Gun, Recoil, Shooting, Bullets, Polish, Game Feel
Id: fCeyiEcWRAs
Channel Id: undefined
Length: 47min 47sec (2867 seconds)
Published: Fri Jul 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.