Make an Action RPG in Godot 3.2 (P3 | collisions + move_and_slide)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to our Godot action RPG series this will be our part three and in this video we're going to be talking about collisions and addressing a few of the comments that you guys gave me in the previous video so let's come into our player here one of the things that you guys brought up and this was a silly mistake on my part to be honest we actually have Delta inside of our clamped function right here which means we're clamping we're multiplying our max speed by Delta right here so if Delta changes that hour then this is going to change we're basically clamping it too soon right Delta needs to be on the outside of this right and so we could put this right on the outside right here and this is this is one step better but this is still got some problems and it's because we're applying Delta to our velocity right but then we're actually manipulating our velocity again later and so that's a problem right there because it's not going to be this velocity is not going to be quite accurate because we've applied Delta beforehand so the easiest thing to do here is just to play Delta at the very end to our velocity and then of course we need to apply Delta to our acceleration and our friction but we can apply it at the very end and one of you also suggested using move toward instead of this right here so we could say velocity equals velocity dot move toward and then we want to give it the value that we're moving towards right and this would be our input vector times our max speed I think if I'm thinking about this right so we take our input vector multiply that by our max speed that's the value we want to move towards and then the amount that we would move towards that would be our acceleration times Delta very similar to our friction right here and this is a lot cleaner and it should work the same theoretically if I've done my job right and yes well access i/o that's another thing I should mention here I've updated my version I'm now running Godot 3.2.1 stable just so you know I did update to that new version and so we're getting we're getting a really slow acceleration here or a very slow Mac speed so somewhere along the line we've changed it right for mac speed is a hundred hour acceleration times Delta velocity equals moved towards it seems to be working how I would expect it everywhere else like it seemed that I was moving properly and stuff so let's try printing our velocity here and see exactly what's happening so we're very slowly approaching we're still increasing our max speed is still increasing and there we go we capped it a hundred and then we slow back down again so here we're we're moving towards this max speed aha yeah so I see what's going on here because we're applying Delta afterwards right to this max speed our max speed is still a hundred that's the value that we're approaching and then we're applying applying Delta to it afterwards and so what we get here is a problem where our acceleration is way too slow for this max speed of a hundred and even though in the end that's going to end up moving the it's going to end up moving slower it still takes us forever to approach that maximum speed that get then gets brought down so let's just try upping our acceleration here I want to just see what this does and there we go we can now hit that max speed significantly faster and we I don't even know what this max speed is going to be like [Music] and why our friction is way slow again of course because we haven't done anything with that so here we go now we've got our movement back similar to how it was before but you can see this does make things a little bit confusing because our friction and our acceleration look much larger than our max speed even though in the end we're multiplying our velocity by Delta to make sure that that's fine it is a little bit awkward and that's something that I've run into but we're not going to get into that too much for now we're gonna leave it about like this because this feels pretty good for a movement it might be a little bit fast for a max speed I think maybe I'll try 80 and see you want your character to be able to move around on the screen pretty quick and AD feels pretty good so we'll use these values but that solves the problem that we had before the last thing that you guys commented that was really helpful is that there's an argument to be made for not using Delta there's an argument to be made that if your game does slow down you want to just have the game to slow down and to show that frame drop and have the character move slower and not be tied to the to the time two seconds to time in the real world and the argument there is that it can be more fair to the player if there's lagging or something then they're going to be able to react better to that lag and deal with it properly so there's two sides of the argument there I'm gonna continue using Delta throughout this because I feel like for this game it will be fine we're not going to end up with a lot of situations where the game is going to lag down significantly so we'll continue using Delta I think this will be best for this game but there is an argument to be had for that and you can might consider that you know that's something to consider when you're working on your game so now let's get right into collisions because that's what we're going to be doing in this is setting up some collisions and we may actually discuss some we'll see yeah let's just do collisions for now so our player character right here is set up as a kinematic body and we've attached a sprite to this but this kinematic body doesn't have any sort of collisions yet so we're going to attach a Collider to this kinematic body and you can see we've actually got this warning error this warning over here saying that it needs some sort of a Collider attached to it or else it can't actually interact with other things so we're going to start by clicking on our player and clicking the little plus button and we'll search for a collision shape 2d we'll just pick this right here for the collision shape for our player I have decided to use a capsule shape and oops actually did I use a capsule shape I hid my other Collider yeah I did okay and this will just make it so that our character can more easily move around corners and stuff if we run into the corner of a wall we'll be able to slide around that a little bit it'll be more forgiving when the player is moving around in our world so if we come over here if you have your collision shape selected and you come over here into the properties we can select new capsule shape and we'll want to shrink this down and let's actually come into our snap options up here and turn on use pixel snap it's probably off because I didn't have it quite set up right and we will oops I accidentally grabbed the player here if you want to select for example the collision shape here and move it without grabbing the player what you can do is you can hold the Alt key you can select it first and then hold the Alt key and that will allow you to move it by itself we'll set it about here and we're actually going to rotate it as well we'll set its rotation to negative 90 so I just came into the transform and set the rotation and that moves it to be more like this I'm gonna hold alt again move it down just a bit shrink this down a little bit more you can set it up how you want we don't want the collision to come up here because later when we deal with depth and making sure that this can walk behind and in front of things well what our collision for movement to be very different from what our collision for taking a hit looks like once we get our hip boxes and hurt boxes set up we'll be setting up a different Collider that encompasses more of the player-character here for that so let's save this like this that looks good enough and let's add in a new node here so that we can test these collisions so we'll come up to world we'll add a new node and we'll do a static body 2d and like I think I mentioned this in the very first video but a static body 2d is used for creating some sort of a wall or static object in your world that can be collided with but won't be moved it's going to be static once we've created that static body 2d you can see it's up here just in the corner we can attach a collision shape to it as well and this time we'll do a collision polygon 2d and when you have a collision polygon 2d attached like this if you select it you'll notice up here that we get some new options right there and we're actually going to turn on snap here just click this little icon right here to turn on snap we can use these options right here to create a collision shape we can just kind of draw it out actually so we'll click this little create points icon we'll create a point here here and just kind of draw some shapes out okay anything you want and come all the way back around and come back to the starting point now sometimes when you do this sometimes when you create this collision shape like this in the collision polygon I've had times where when I come all the way back around it doesn't fill it in um you should see it fill it in like this if it doesn't fill it in you'll need to right-click and try again because that means it didn't quite work right and that's happened to me a few times I don't know exactly why but usually if I just do it again then it fixes it now we've got this setup if we run our game you can see we can move and we can actually collide with these but it's it's not it's not properly you can't see it right we can't see the collision shape so what we'll do for now is just come into debug and do click visible collision shapes and you can see there's some other debug options here but visible collision shapes should be good for now now when we run the game we can see the collision shape for our character and the collision shape for the walls but you'll notice we kind of stick to these walls a little bit if we move into them it feels kind of like they're sticky we have a hard time getting away from them and that is because of the function that we're using for our character's movement we are using move and collide right here move and collide handles movement and collisions force if you notice we didn't have to write any collision code it just automatically worked for us once we put our colliders on right that's wonderful but the problem is that it doesn't slide along those walls and there's a couple different solutions for this if you come into the help file and look up move and collide right here in our kinematic body you'll see that there's another function right under it called move and slide and so this function right here can handle sliding along an edge as well and it comes with some other properties that you can pass into it if you're making like a platformer game or something but since we're kind of making more of like a 2.5 D perspective we don't need to worry about a lot of those optional arguments that you can pass in so we could just switch this right here to move and slide and run the game there's gonna be a problem though and the holy cow we're moving way slower now right that's not good why are we moving so much slower well come back here and look at move inside you can see that for move and slide if we come down to here it says the linear velocity this is the argument that we're passing in right it says is the velocity vector and pixels per second unlike a move and collide you should not multiply it by Delta the physics engine handles applying the velocity so if we come back into our player right here and we say we can now remove the multiply by Delta and that's because move and slide will handle the Delta for us inside of the function so we don't have to do it outside we still have to have Delta applied to our other areas but we don't have to have it applied there you can see now it's a lot smoother we don't stick to these walls like we used to before we slide along them in the corner here we get a little bit of weirdness going on right here and one of the things that I generally do with the move and slide function is it actually has a return value and if you guys come in to your warning messages right here so let's talk about warnings for a bit and get Oh Godot will give you warnings they're not necessarily game breaking bugs it's not going to break your game however these warnings will often tell you about potential issues you might be having so this warning for example says the function move and slide returns a value but this value is never used so they're saying this function returns some sort of value but it's not being used currently so if we come back into our kinematic body we see that move and slide returns a vector - okay so what does that mean let's come down and read it says returns the linear velocity vector rotated and or scaled if a Salida collision occurred to get detailed information about collisions that occur get use get slide collisions so this is if we need to get more information out of the function but for now it's just saying this returns a linear velocity so that means we're passing in a linear velocity and then this says oh you're velocity this should be your update of the velocity after the collisions so in order to get that information we just say velocity equals move and slide so now this function returns some sort of velocity after a collision and we set that to be our new velocity and that right there is will allow us to remember the velocity that we had before the collision and properly handle that which seems to fix the issues we were having in these corners you can see and we also kind of keep our momentum around corners will kind of slide up the new edge and that's because we're getting that information from the function it's saying oh look you got this you had a collision and this is the velocity that was left over from the collision and now set that to be our new velocity so there you go and that talks about warnings as well if you see we've got another warning right here font oversampling only works with the resize modes keep with keep height and expand so I'm assuming that that means when we start using fonts if the fonts may start having issues unless we use if we come into our project settings and our window unless we use the aspect ratio here we might want to set this to keep I wonder if that would do it or if we need to set it to one of those keep with keep height setting it to keep seems to have fixed that and usually keep is what I like to do that way it just keeps either the width or the height depending on which one fit better and that will just add black bars to the screen if the if the aspect ratio of the monitor or the phone or whatever doesn't quite fit our 16 by 9 aspect ratio of the game so that's gonna be it for this video thank you guys so much for watching it I hope you enjoyed it once again let's continue this discussion if you guys have feedback for me or comments leave them in the comment section and I will get to them I've been answering and reading every single comment your comment may end up helping with somebody else or in a future video that's what we're here for we're all learning and I hope you enjoyed this video and I will talk to you all later
Info
Channel: HeartBeast
Views: 155,734
Rating: undefined out of 5
Keywords: Godot Engine, Godot 3.2, Godot 3, Tutorial, Series, Action RPG, RPG, Pixel Art Game, Indie Game, Game Development, Learn gamedev, Gamedev
Id: TQKXU7iSWUU
Channel Id: undefined
Length: 18min 14sec (1094 seconds)
Published: Tue Mar 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.