Hello, forum, It's been a while since my last inquiry on this forum "IRL is quite busy this year", so I continue working on my game projects in my spare time. However, I've encountered a challenging hurdle related to the bullet mechanic. My intention is for the bullets to be fired from the front of a gun sprite, and I've attempted to align the bullet spawn script with the Bone2D of the hand on the sprite. Unfortunately, the outcome has not met my expectations.

I'm currently seeking guidance on how to make the bullets shoot toward the cursor in a manner similar to how I programmed the Bone2D hand to follow the cursor's position. Any advice or assistance would be greatly appreciated.

Also, this is how the script is written.

extends Bone2D

var shooting = true
var bullet = preload ("res://sprite/char/tm_a1/bullet_test.tscn")

func _physics_process(delta):
	
	if Input.is_action_just_pressed("left_click"):
		var bullet_instance = bullet.instance()
		bullet_instance.rotation = rotation
		bullet_instance.global_position = global_position
		get_parent().add_child(bullet_instance)
  • Thank you for all of your guy's answers, but It looks like I figured it out, the code I shared before is just for bullet spawn not the behavior of the bullet, but I'll share the code I use anyway, feel free to use, add, or optimize code I used ^^

    extends Area2D
    
    export var speed = 4000
    var direction = Vector2.ZERO
    export var deletetime = 0.6  # Customize this value to set the time before deletion in seconds
    var timer = 0.0
    
    func _ready():
    	set_as_toplevel(true)
    	
    func _process(delta):
    	if direction == Vector2.ZERO:
    		# Get the target position (cursor position) when direction is not set
    		var target_position = get_global_mouse_position()
    		direction = (target_position - global_position).normalized()
    	
    	# Calculate the movement vector using the stored direction
    	var movement = direction * speed * delta
    	
    	# Update the position by adding the movement vector
    	position += movement
    	
    	# Rotate towards the cursor
    	rotation = direction.angle()
    	
    	# Update the timer
    	timer += delta
    	
    	# Check if the timer exceeds the deletetime
    	if timer >= deletetime:
    		queue_free()
    
    func _physics_process(delta):
    	yield(get_tree().create_timer(0.01), "timeout")
    	$small_bullet_1.frame = 1
    	set_physics_process(false)

First make sure that the node position is set to 0.
You can put a position2D and tell it that the global_position of the bullet is equal to the global_position of the position2D (put the position2D where you want the bullet to be instantiated).

Thank you for all of your guy's answers, but It looks like I figured it out, the code I shared before is just for bullet spawn not the behavior of the bullet, but I'll share the code I use anyway, feel free to use, add, or optimize code I used ^^

extends Area2D

export var speed = 4000
var direction = Vector2.ZERO
export var deletetime = 0.6  # Customize this value to set the time before deletion in seconds
var timer = 0.0

func _ready():
	set_as_toplevel(true)
	
func _process(delta):
	if direction == Vector2.ZERO:
		# Get the target position (cursor position) when direction is not set
		var target_position = get_global_mouse_position()
		direction = (target_position - global_position).normalized()
	
	# Calculate the movement vector using the stored direction
	var movement = direction * speed * delta
	
	# Update the position by adding the movement vector
	position += movement
	
	# Rotate towards the cursor
	rotation = direction.angle()
	
	# Update the timer
	timer += delta
	
	# Check if the timer exceeds the deletetime
	if timer >= deletetime:
		queue_free()

func _physics_process(delta):
	yield(get_tree().create_timer(0.01), "timeout")
	$small_bullet_1.frame = 1
	set_physics_process(false)