GameMaker Studio 2 - The Basics of Collisions Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] greetings and welcome my name is Aaron Craig with be honest games in this game maker studio tutorial I'm going to be showing you the basics of collisions now game maker studio has split this up into two types of simple collision and advanced collision checking so I'm going to do the same I'm going to split this into two different tutorials if you know about collisions and you've been using simple collision checking for a while then go ahead and jump on over to collision basics for 102 I'll put a card up for that and if you haven't then this one is a great place to start let me show you what I'm going to be teaching about in this simple collisions despite their name are very powerful and they are just simple to use but they're still very useful okay so we're going to have a project where we have a character that can walk around and this brick wall right here is going to be something that we can run into but that we can't get stuck in at all and we're going to have this blue wall which is like a ghost wall that we can move right through and then the tree that we can't hit the base but we can go behind it okay that is what I'm going to be showing you how to set up so let's go ahead and jump right in these functions I'm not going to walk through one of the time because I don't think you need to see them that way so what I want to show you most is placed free as I find it the most useful it's a function that just takes two arguments an X and a y-coordinate and it just checks to see is that place occupied by an object that is flagged as solid if it is do not move there if it's not then you are free to go so I'm going to show you how this can be super useful when you are moving around especially for a player character so they don't get stuck on objects let's get started on that so we're going to create a sprite we're going to come in here and this is going to be a very simple sprite because I am NOT an artist we're going to create that we're going to create a straight line and a little bit larger just like the ones I showed you might be a little different with the stripes but hey that's okay we're going to exit out of this and we're going to name this SPR brick wall and we're going to Center this and we're going to come in we're going to make an object with it as well so create an object obj brick wall assign it that sprite and then we're going to go into our room and we're going to put that brick wall in and just going to assign it all over here and I got the ordered a little bit out okay there we go and we'll put one right there now if we run this nothing is going to happen yet because it's an object but Sarah's going to go right behind it because it's the last object added and nothing happened it's not solid it's not collision checking even though they're both objects nothing is actually happening yet and so now let's go and fix that problem so let's go ahead and check on Sarah she is marked as solid but the brick wall is not now marking something as solid is only useful in the collision detection so if you have looked at the events at all you know that there is an actual collision here so we could come in here and on either one the brick wall or on Sarah we can add a collision and if we do that because they're marked as solid it is actually going to work now so she will run into this and not be able to go through it at all now here's a slight problem if I'm holding over and I try to move down or up she is not moving she's actually stuck on this wall because the collision detection only happens when the two objects are actually running into each other so they are colliding and then she can't go up or down because she is in mid collision with this brick wall and we don't want that to happen it is very janky movement and does not feel good for the player so we're actually going to delete this because we don't want it to be a physical collision we don't want them to actually be touching it instead we're going to come into objects Sarah and we're going to go to her create event and we are going to add in a variable this is going to be called collision speed and I'm going to put it equal to walk speed plus two and this will make sense in just a second as we're going to jump over here and what we want to do is use this place free function so we're going to put in two ampersands which is also and either one works and we're going to say place free now we just need an X in the y coordinate so the x over pressing on D is we're going over and you can tell that because it says X plus so we're going to do X plus we're going to do collision speed now the reason that we're using a collision speed is because if we use the walk speed there can still be instances when we are actually trying to go in a certain direction going up up and over or left and down or something like that and we still are running into it too much because we're checking the exact amount of pixels we want to move and it can still cause issues but if we are checking just a little bit just a fraction beyond what we are attempting to move then it'll still look great and we won't run into any of those errors so we're going to go ahead and copy this and just check it for every direction that we're moving so we're going to do X minus collision speed Y and place is free we're going to do Y X and then Y minus collision speed and we're going to do and place free X Y plus collision speed save that and now let's go ahead and run it Sara will run into these bricks but she won't be able to get stuck anymore so I'm moving over and it looks great no longer you can see that she has just a fraction like two pixels there that she's not running into it but I still think that looks great if you need even more than go down to one and if that's even too much then you can alter your sprites to actually make them work better so that is using place free a simple collision detection now place free works on the instances as they are marked solid now if they are not mark solid then it won't work so let's go ahead and create another sprite will call this ghost wall will fill it up blue and then we'll do a little bit of transparency and just mark it like this ok now let's Center it create an object for the ghost wall assign it that sprite and do not mark it solid come into the room place it right there and we'll run the game now Sarah will pass right through it because even though she is solid the function that we are checking plays free checks to see if the instance that we are going to run into a solid and this one is not so that is a simple collision detection for images and objects actually running into each other now the way that it works is they are marked solid but the actual collision detection is being done on what's called a collision mask so if we open up our sprite brick wall right down here is a collision mask and it is normally set to automatic right here but if we wanted to we can actually come in here we can do it manual you can change it to an ellipse a diamond precise which would actually cover the exact model of the sprite like if we came up to Sarah and we went to the collision mask and we said precise you can see here that it actually goes all exactly around her no longer does it cover the entire frame as it would if we did like a rectangle where it covers even the empty space it only covers her now you can tell that it says slow because it uses more processing power so if you do this on a few objects it's going to be okay if you do it on every single one and you have a lot of sprites in your game it might start slowing it down okay so I'm going to leave that as rectangle for her but if we come up to the brick wall and we can actually alter this we can come in here and I set that just in the middle of it well now all of a sudden even though the sprite hasn't changed at all we're actually able to go into this wall because that collision mask is what our detection is looking for and the collision masks can be altered even while the game is running you can change collision masks now let me show you something really cool if we import the sprite so I have a sprite tree now I'm going to make an object for this you don't have to follow along in this part let me just show you something really cool and how I did the thing with the tree in the beginning we're going to set that there we're going to go back to the sprite and on the collision mask for the tree obviously you might not want to have them be able to run into the whole tree because it doesn't make sense a tree goes up as we know even the 2d game even if it looks like it the player should be able to go behind the tree and you can see here that I've moved the collision mask just to cover the base of the tree and when it is covering only the base then the player can walk behind it and that makes for a much more realistic collision when it comes to objects that are up in the sky you alter the collision mask to then make it so that they can move behind it or in front of it however that it should look in your game now inside of the tree if you wanted to have it fade like I showed in the beginning it's a really simple thing and it covers the more advanced but let me show you a practical use for it inside of the step we can actually come over here and all we have to do is say if collision circle which if you're wondering how this works do it at the end of this jump over to 102 and I will explain it even more we're going to say X Y 15 obj Sara true true we're going to set the image alpha equal to 0.25 and then else image alpha will equal one and then inside of our room we'll throw this tree and run the game and you'll see once again like at the beginning we're able to walk behind it and make it go invisible as we do okay so that's part of the advanced collision checking that you can do and we will get into that as we go along now I did not mark this tree as solid so we're able to walk along the whole thing which is very awkward but that's what I've got for you guys that is the basics of collisions you need to have your objects marked a solid and then you can run into them and you can do awesome things like this collisions are super important in your game any any game you make will probably be using collisions whether that is a platformer an RPG shoot them up it doesn't matter your objects are going to be running into each other and they need to know how to react and where to go and where they are not allowed to go hopefully this was helpful and that's all I've got for you guys thanks for joining me and as always have fun making great games and I will talk to you later hey there I've got a patreon if you didn't know if you'd like to support me that would be great up on the screen are the people who are pledging enough to get their name in the credits they are helping fund this YouTube channel which is awesome I just want to give a shout out to them and all that they do to help me do this it's great if you would like to join you can click on the link at the end of the video or in the description below thank you [Music]
Info
Channel: Let's Learn This Together
Views: 85,773
Rating: undefined out of 5
Keywords: Game Making, Making Games, Game Maker, Game Maker Studio, Beyond Us Games, Game Maker Tutorial, Game, Maker, Studio, Tutorial, Training, Learn Game Maker Studio, Learn Game Maker, Learn Game Making, beginner, tutorial, beginner tutorial, newb, newbie, 101, basic, basics, training, collision, collisions, gamemaker collision, functions, function, how to, collide, solid, solid objects, what is solid, BUG, making a game, learn game dev, gamemaker 2, indie game devlog, gamemaker studio 2 tutorial, dev
Id: yDBRSwS4vXw
Channel Id: undefined
Length: 12min 40sec (760 seconds)
Published: Wed May 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.