Need help with Node direction firing a bullet

4.2.2 Stable

Hello, I am trying to spawn a bullet from a node that is facing the same direction as that node. The node itself is going to be attached to another node that will follow the mouse. But I would like the bullets to fly out at an angle to the mouse aiming/direction

– this works fine, but will only fire towards the mouse, no “Spread Shot”
extends Node2D

@export var bulletScene : PackedScene
@onready var bullDirection
@onready var LeftSpawn = $LeftCannon
@onready var RightSpawn = $RightCannon

func _ready():
pass

func _process(_delta):
bullDirection = (get_global_mouse_position() - global_position).normalized()
look_at(get_global_mouse_position())

func _unhandled_input(event):
if (event.is_action_pressed(“Primary Fire”)):
var bulletL = bulletScene.instantiate()
var bulletR = bulletScene.instantiate()
spawnbullet(bulletL, LeftSpawn)
spawnbullet(bulletR, RightSpawn)

func spawnbullet(bullet, marker):
get_tree().get_root().add_child(bullet)
bullet.global_position = (marker.global_position)
bullet.direction = bullDirection
bullet.rotation = bullet.direction.angle()

Ive tried to separate it into two chunks, one “Aiming” scene

extends Node2D

@onready var bullDirection

func _ready():
pass

func _process(_delta):
bullDirection = (get_global_mouse_position() - global_position).normalized()
look_at(get_global_mouse_position())

works fine. Rotates the needle towards the mouse. Good Great! now to add individual bullet spawners as children so they point in formation at the cursor but can face outwards…

extends Node2D

@export var bulletScene : PackedScene
@onready var bullDirection
@onready var spawn = $SPAWNPOINT

func _process(_delta):
bullDirection = spawn.rotation

func _unhandled_input(event):
if (event.is_action_pressed(“Primary Fire”)):
var bullet = bulletScene.instantiate()
spawnbullet(bullet, spawn)

func spawnbullet(bullet, marker):
get_tree().get_root().add_child(bullet)
bullet.global_position = (marker.global_position)
bullet.direction = bullDirection

This is where it breaks down. The bullets either wont move or won’t spawn without crashing the game. For some reason a nodes direction isn’t a vector 2d or something.

The bullets code always throws an error on the move and slide function no matter what I try and supply it with.

extends CharacterBody2D

const speed = 300
var direction = Vector2.ZERO
@export var SmokeScene : PackedScene
@export var bulletImpact : PackedScene

func _ready():
pass

func _process(delta):
var collisionResult = move_and_collide(direction * speed * delta)
if collisionResult != null:
var smoke = SmokeScene.instantiate() as GPUParticles2D
get_parent().add_child(smoke)
smoke.global_position = collisionResult.get_position()
smoke.rotation = collisionResult.get_normal().angle()

var impact = bulletImpact.instantiate() as Sprite2D
get_parent().add_child(impact)
impact.global_position = collisionResult.get_position()
impact.rotation = collisionResult.get_normal().angle()
queue_free()

I literally just want it to shoot the damn bullet in the direction the spawner is facing… This seems needlessly complicated.

NeverMind! Its Global Transform.x The vector2 output of a nodes direction is global_transform.x Boy that’s not obtuse at all.

1 Like

Why not just copy the rotation? Is there something wrong with that?

bullet.rotation = shooter.rotation

You can rotate it a little bit for bullet spread after this assignment. I recommend randfn

You can use raycast group for spread bullet