The Weapon
This content is not available in your language yet.
Let’s give our player something to attack with!
Weapon Scene
Let’s make our weapon!
-
Create a new scene for our weapon! Give it a Root node of a Node2D and name it “Weapon”
-
Give it two children, a Sprite2D and a Animationplayer Give the Sprite2D a child of type Area2D and give the Area2D a child of CollisionShape2D
Here’s how my scenetree looks:
Make sure you save the scene, calling it “Weapon.tscn”
-
Let’s start by picking a sprite for our weapon, so we can see what we’re working with! Select the sprite node, and in the Empty texture field, select Load. Navigate to your assets folder, and select a weapon that you like!
Don’t worry that it’s pointing up, we’ll rotate it in a minute.
-
Let’s open the CollisionShape2D node and assign a shape, you’ll probably just want to use a rectangle shape. This will be the hitbox of the sword, and determine whether an enemy has been hit! Adjust its bounds so that it vaguely matches the sprite. Although you’ll likely want to make it a little bigger than the sprite, as we don’t want our game to feel like the player has to be too precise.
-
let’s rotate the Sprite2D node. Do this by selecting the node in the scenetree, navigating to the inspector, opening the Transform tab and changing the rotation to 90
Let’s also offset it’s position a little, to help it rotate around our player smoothly. Set its x-position to 20px
Here’s how my sword scene and inspector look:
-
Before we move on, navigate to the inspector tab of the Area2D node and find the Collision tab, under Layer make sure nothing is selected. Under Mask Make sure only 2 is selected.
Weapon Script
-
Let’s create a script to control our weapon, create it using the default template and attach it to the root node2d, call it “weapon.gd”
We’ll need this script to do two things:
- Rotate around our player, facing the mouse.
- Play the animation when we press our attack input.
-
The first step will be nice and easy! In the process function, add the line
This will cause this node to always face toward the position of the mouse!
-
Next, we’ll need to get a reference to our AnimatedSprite2D node, we’ll do this the same way we did for the animated_sprite_2d node for the player’ by clicking, dragging, and then holding ctrl before we let go of the click.
We’ll also want a variable that keeps track of if we’re currently attacking, as we don’t want the sword to destroy enemies when we haven’t attacked.
your script should look like this:
-
Now, let’s add a check to see if we’ve just used the Attack input action we created earlier, and play the animation we’ll create next. This is all stuff we’ve done earlier, so this should be pretty easy. Here’s what it’ll look like!
Making sure that the Input action name, and animation name match, including case sensitivity.
Sword Animation
Now, let’s start creating our attack animation!
-
Navigate to the Animationplayer and click Animation and create a new animation. Call it “Attack”
-
Now that we’ve created a new animation, we’ll need to create an Animation Track which you can do by clicking Add track. This will ask us what type of Animation Track we want to create.
We want to animation the Position of our weapon, this is a Property so we’ll create a Property Track.
Then, when prompted, we’ll select the Sprite2D as this is what we want to change the position of!
Finally, scroll until you see the Position property.
Phew! That was quite a few steps, but our animation track is created!
-
Now we need to determine the keyframes our sword will animate between. We’ll need three: The first being the start position, the second being the extent of the attack, and the third being returning to the start position.
To add a keyframe, right click on the animation track. (In the Position Row) and press Insert Key do this until you have three keyframes. If we select each keyframe, we can modify their values!
The first and last keyframe, we want to be at Time 0 and 1.0 respectively, with their values being unchanged (Remember, we want both of these to represent the sword at rest)
Our second keyframe, we’ll for now put at a Time of 0.5. Let’s however, set its x-value to 30.
-
Great! Let’s hit the play button on the animation, and you’ll notice we have a simple stabbing animation. But it’s a little slow… We can fix this by adjusting the total length of the animation, this can be done over on the right.
Change the length from 1.0 to 0.5. We’ll then need to adjust our keyframes, adjusting the middle one to be at a Time of 0.25, and the last to be at 0.5.
Play it again, and you’ll notice it’s much faster!
-
We’ll want to add another Property Animation Track this time to modify the attacking variable, to keep track of if our weapon is “Active” or not.
Add a new animation track, select property, and you should see “attacking” right at the top! Add two keyframes, one right at the start, and one right at the end. The first we’ll want to set the value to “on” (as we’re now attacking) and the one at the end will set the property back to “off” (or unticked)
And that’s it! We’ll come back later to add the code for destroying enemies.
Great! Let’s get our weapon added to our player!
Adding our weapon to our player
Navigate to your player scene. From the filesystem, drag in your weapon scene (called weapon.tscn) and attach it as a child of the main CharacterBody2D node.
Play your game, and you should hopefully have a weapon that rotates around the player and stabs when you click it! If it doesn’t seem to be rotating around the middle of the player sprite, feel free to adjust its position within the Player scene.
You’re also welcome to adjust the size of the weapon (Although, you’re best to do this within the weapon scene itself) it’s your game after all!
Checklist
- I’ve created the weapon scene
- I’ve created the weapon script
- My sword sprite has an animation
- I’ve added the sword to my player scene