Create a new scene with Node2D as root
Add a Timer Rename to CoolDownTimer
Why use a Node2D as the root for the Gun?
1. When to use a Specific Root (The Exceptions)
A. Physics Actors (Player, Enemies, Bullets) If an object moves via physics (collisions, gravity, velocity), the Root Node must be the Physics Body itself.
Why: If you make a Node2D the root and put a CharacterBody2D inside it, when you call move_and_slide(), the Child moves away, but the Root stays at (0,0).
Result: Your game logic (position, calculations) thinks the object is at the starting point, but the visual sprite is miles away.
B. User Interface (UI) If you are making a menu, health bar, or inventory, your root should almost always be a Control node (or a specific container like VBoxContainer).
Why: Node2D does not have anchors or margins. If you use it as a UI root, your interface won't resize correctly on different screens.
2. When to use a General Node2D Root (The Rule)
For almost everything else—especially "Composite Objects"—the generic Node2D approach is superior.
Weapons/Tools: (Guns, Swords, Flashlights)
Static Level Objects: (Trees, Chests, Signs)
Visual Effects: (Explosions, Magic Spells)
Spawners: (Enemy Spawners, Checkpoints)
The "Composite" Benefit: Using Node2D allows you to treat the object as a container. You can swap the art (Sprite), change the sound (AudioStreamPlayer), or add particles without breaking the logic on the root node.
Select the Sprite2D
Set the texture to lasergun.png
Set the Filter to Nearest
Select the Marker2D
Place it at the Muzzle of the Gun
Add a script to the Gun node
Add an export variable for
Shooting Speed
Add a constant for the bullet scene
Add references to :
The marker
The timer
The Sprite
Include variables for can shoot and bullet direction
Create a function to set the direction of the gun
Set the bullet to point in the direction of the gun
Rotate the gun to point in that direction
Flip the gun 180 if it has rotated to be upside down
This function does two jobs: it updates the Logic (saving the direction for the bullet) and updates the Visuals (rotating the gun sprite and flipping it so it's never upside down).
Connect the CoolDownTimer timeout signal
Code it to allow the gun to shoot
Create the shoot function
If we can shoot
set can shoot to false
Set the wait time of the cooldown timer
Start the timer
Set the position of the bullet
Set the direction of the bullet
Add the bullet to the scene