How to equip and animate weapons in 2D - Godot (the EASY way)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's arki today i want to wrap some loose ends from my character customization tutorial in that tutorial i showed how to change some of your players sprites so you can create a fully customizable character and change stuff like the hair or clothing go check it out i'll leave a link in the description now i want to go over attaching items to the hand of your character and also being able to animate those items right here you can see me being able to switch items that my character is holding this is a game that i'm developing and it's called miners delight you can check out my first devlog to see more in my pixel editing software first notice that i keep each component on separate layers this is so that i can stitch them together later on and be able to customize each component easily when making something like a sword you have two options one is that for every frame of an animation you can draw the sword however this can be really time consuming especially if you just want to have different weapons and items you want to hold then you would have to draw out several sprites for every different weapon you have say for instance you have 8 different animations and each of them have 8 frames so that means for one weapon you would have to draw it 64 times if you have two then you would have to draw 128 frames you can see how it can easily get out of hand the method that i use in my game requires you to draw the weapon just once and then we'll use the game engine to animate the position and rotation of the sprite in this way we can also reuse the same animation for multiple different weapons effectively cutting down the work of adding new weapons to be just drawing it once once you have all your layers and their respective animations drawn let's export each of them into their own sprite sheets for this tutorial i'll just use the idle and walking animations for demonstration and for the sword and pickaxe i'll just export it as their own png file because we'll be animating those within the game engine alright now here in godot i've added all the sprite sheets and images to the project folder i'm going to create a note 2d and then create a kinematic body duty for the player scene within it i'll save the player as its own scene since the player is a kinematic body type i'll add a collision shape 2d now i'll also add a note2d as a child to encapsulate all the sprites that we'll be working with and within it i'll create sprite nodes for all of the layers make sure to set the v frames and h frames property to match the number of rows and columns in the sprite sheets for the on hand item sprite i'll temporarily add the sword image to its texture property don't worry later on we'll be able to change the texture with code okay so i'll save it and try running it and yeah it's just too small so i'll scale up the player i'll just set the scale up to three for x and y direction and great we can see it with all the clothes now i'll add an animation player to animate our character i'll start off by doing the left idle position i'll make the length 0.8 seconds and i'll set it to be looping i'll keyframe the frame property from 0 to 7 and i'll do this for all of the sprite nodes except for the on hand sprite since it does not have a sprite sheet [Music] now that we have set up the idle animation let's enable the play on low property in the animation player so we can see the animation when we press start and great we can see our player animating except for the sword which is not in the right place so let's set that up to correctly set the sword up let's make sure the animation player is set to the first frame now let's drag the sword into the right position and also rotate it to face downwards but before that let's set the pixel snap and also rotation snap since we're working with pixels and don't want any tearing notice how the sword handle is being rendered in front of the arms to fix this let's drag the on hand sprite before the arms node so that it is rendered below the arms also let's make sure to keyframe the position and rotation of the on hand sprite now let's click play and great now we can see our player with the sword in the correct position now let's try doing the walking animation it's the same process as the idle animation but the frames we key are going to be from 16 to 23. let's enable the play on load for the left walking animation and click play our player is walking now but the sword isn't moving alongside the arms let's fix that for each frame within the animation we want to change the position and rotation of the sword to match the hand's position we key these properties for each frame and then we can have the sword move as expected [Music] [Music] make sure to change the animation property for both the position and rotation to be discrete as keeping it continuous will cause movement in between frames and we don't want that let's click play and check it out nice we got the sword to move [Music] now let's try replacing the sword with the pickaxe and see [Music] this also works so essentially we can create as many different types of swords or pickaxes and not worry about doing the walking animation for them if we want to be able to change the character's item on hand during game run time we need to do it through code so let's add a script to the player node i'll store the sword and pickaxe textures in variables by preloading them then i'll implement the unhandled input function so we can try to change the item the player holds through button presses i'll go to the input map tab within project settings and i'll add the action change equip i'll assign a key space for instance and now when the change equip action is pressed i want to change the texture of the item that the player is holding so first i need to get access to the on hand sprite to be able to change it so i'll create a variable for that and now i'll try to change the texture on second thought i'll add two actions each corresponding to holding the sword and holding the pickaxe so i have changed the sword and i'll set that to be q and then i'll also add change to pickaxe which i'll set to be e and now i can do an else if statement and then if the action press is changed to pickaxe i'll set the sprite to be the pickaxe texture i'll click play and let's see great now i'm able to switch between pickaxe and sword on keypress with this if we want to add a new sword we can just quickly draw one and add it to our game and that's all for today if you made it to the end i appreciate you and if you found this video helpful drop a like and consider subscribing till next time [Music]
Info
Channel: Arkeve
Views: 41,296
Rating: undefined out of 5
Keywords:
Id: GPYBNdYuSD8
Channel Id: undefined
Length: 8min 36sec (516 seconds)
Published: Sun Jul 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.