Placing and Orienting Cannons in Space

The first step is to make a way to place cannons in space. We’ll use an editor-only scene that isn’t visible in-game, so we make a tool script to draw where the cannon is pointing. We make a cannon configuration by adding more of these locator nodes.

Positioning and rotating the emitters

The Position2D node draws where its position is in a scene, but we want to extend it so it’s easy to see its orientation too. I draw a big X at the end of a thick line for clarity.

Either way, a tool script with the _draw function will make it easier to see in the editor.

src/FiringConfigurations/WeaponLocation.gd

tool
extends Position2D


func _draw() -> void:
    if Engine.editor_hint:
        # Line from origin to 100 pixels ahead
        draw_line(Vector2.ZERO, Vector2.UP * 100, Color.steelblue, 4)

        # Red and green X formation lines 30 pixels long on either side of the point of the line
        draw_line(Vector2(-15, -85), Vector2(15, -115), Color.red, 4)
        draw_line(Vector2(-15, -115), Vector2(15, -85), Color.green, 4)

We turn it into a blank scene that only contains this new Position2D script for quick instancing when making configurations.

src/FiringConfiguration/Weaponlocation.tscn

One cannon facing forward

A configuration is a Node2D that contains one or more Position2D nodes. We start simple with a single cannon that points forwards.

Note the distance from the origin; I use the size of the ship in my demo, but you can temporarily import your character’s sprite to know where the cannons should be.

These configurations are scenes and should go in a base resource folder for your weapons system.

FiringConfigurations/Straight1Configuration.tscn

Other configuration ideas

Three cannons in a cone

FiringConfigurations/Cone3Configuration.tscn

Four-way cannon

FiringConfigurations/Cross4Configuration.tscn

Double cannon with aft helper

FiringConfigurations/Front2Rear1Configuration.tscn