Make bullets fire in the direction a ship is pointing

Godot Version

most recent

Question

Hi
I have a problem with a turret, which is a child node of a ship. I can fire from it and the bullets will go along the negative z-axis and also kind of up or down depending on in which direction the ship is facing. They wont at all go left or right though. I messed around a bit with combing various values but cant find a good solution. Reverted to just shooting straight.

Does anyone have any ideas?

extends Marker3D

const BULLET_SCENE = preload(“res://BULLET_TEST.tscn”)

func _process(delta):
if Input.is_action_just_pressed(“ui_accept”):
spawn_bullet()

func spawn_bullet():
var bullet_instance = BULLET_SCENE.instantiate()

# Set the bullet's global transform to match the turret's (spawn position)
bullet_instance.global_transform = self.global_transform

# Access the ship's global transform (the ship is the parent of the turret)
var ship = get_parent()
if ship:
	var forward_direction = -ship.global_transform.basis.z  
	
	bullet_instance.direction = forward_direction

var bullets_parent = get_node("../../Bullets")
if bullets_parent:
	bullets_parent.add_child(bullet_instance)

Please use preformatted text with ``` for code snippets.

What is this direction parameter?
And how do you want the bullets to go left or right? Do you want to steer them directly? Or randomize the direction?

1 Like