Hello, indie devs!
Now, before we go crush zombies, the player needs a weapon, right?
Fear not! I am going to show you how to make lasers.
I am using an area2D, as it isn't supposed to react to any physics, but it needs to detect collision. I added a collision shape and a collision shape to match it.
Now, the laser only has to do three things- move in one direction, damage an enemy if it hits one, and leave the screen once it hits something. Here is the basic code I have for that, minus the enemy attacks, that is coming.
You can adjust the speed of the laser as needed, or whatevr in the inspector. Now how do we shoot them?
Remember how the player move on button pressed ui up, ui down, and all that stuff? We can add a new Input for attacking, and assign it to the lasers. To make an input, go under Project -> Project Settings, and then Input Map.
From there, at the top of the panel, there is a text bar next to the word Action. You can type in a name for your action, and click on add to add it as a new input.
After that, locate the new input in the list, and select the little plus button to the right of the new input.
There are different settings for each type of input. I'm going to use the space bar, so I will click on key. It asks me to press the key that I want to use- space. After that, I click on OK. Do not press enter, or else you will switch your input to the enter key.
And, there you have it! Your very own custom input!
But how do we use it?
First, we need to make the laser its own scene, so right click on it and add the branch as new scene.
Next, in the Player Script, add this line of code in the get_input function:
However, we have one issue. When we use Attack after pressing play, the laser follows the player's movements. How do we fix that? If we now use remove_child, it does not work.
The issue is that the child is following its parent node, so we need to find a parent that is not going to move... like the root node.
Now, when you shoot the lasers, in your SaveData, you would want to subtract a "bullet" from your stash.
Your code would then look something like this.
Also, a nice little trick you can do with lasers is add a shadeless canvas material, and a light2D.
Have fun making your weapons, and I will see you in the next one.