Create a new scene with Area2D as root
Add a CollisionShape2D as child
Add a VisibleOnScreenNotifier2D as child
Add laser.png as the texture
Set the filter to Nearest
Add a scrip to the Bullet
Add variables for:
speed, damage, direction
Set the direction of the bullet
Rotate the bullet to face the direction
In the set_direction function, the bullet is rotating to align its local X-axis (the "Right" side) with the direction it is traveling.
Here is the breakdown of why this happens:
Godot's Default "Forward": In Godot's 2D engine, 0 radians (0 degrees) is defined as pointing along the Positive X-axis (Right).
angle(): The .angle() function calculates the angle between the Positive X-axis (Right) and the vector you provided (bullet_direction).
The Result: By setting rotation = bullet_direction.angle(), you are telling the bullet: "Turn from facing Right until you face the direction you are moving."
Moving Right (1, 0): Angle is 0. Rotation is 0. (Bullet faces Right).
Moving Down (0, 1): Angle is 90° (approx 1.57 rads). Bullet rotates 90° clockwise.
Moving Left (-1, 0): Angle is 180° (approx 3.14 rads). Bullet flips to face Left.
In the physics process function
multiplying speed by direction by delta
and adding it to the position
Connect the body entered signal
Code it to damage the enemy
Connect the notifiers screen exited signal
Code it to destroy the bullet once offscreen