How to add guns

I really want to add guns to this 2d game I’m working on and I have sprites (32x32) I watched a video and tried to add them but couldn’t. Can anyone help how to? I have a whole asset but I just want to add a pistol that works. Here is the current movement code:

extends CharacterBody2D

var speed = 200
var health = 100
const jump_velocity = -400
var direction = Vector2()
var jetpack_fuel = 100
var points = 0

@onready var sprite_2d = $Sprite2D
@onready var fuel_label = $Camera2D/jetpack_fuel 
@onready var health_label = $Camera2D/main_health
@onready var health_bar = $Camera2D/health_bar
@onready var fuel_bar = $Camera2D/fuel_bar


var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation = 'idle'

func _ready():
	health_bar.value = health
	fuel_bar.value = jetpack_fuel
	
func damage(delta):
	if Input.is_action_just_pressed('Fire'):
		health -= 50 * delta
	health_bar.value = health
#Function for moving

func _jetpacking(delta):
	if Input.is_action_pressed("Jump") and jetpack_fuel > 0:
		velocity.y = jump_velocity * 0.5 
		jetpack_fuel -= 10 * delta 
		animation = 'jetpacking' 
	else:
		velocity.y += gravity * delta 
		if not is_on_floor():
			animation = 'jumping' 
	if not is_on_floor():
		speed = 250
	fuel_bar.value = jetpack_fuel

func _walking_n_running():
	direction = Input.get_axis("GoLeft", "GoRight")
	if direction != 0:
		velocity.x = direction * speed
		if speed > 200 and is_on_floor():
			animation = "running"
		if speed <= 200 and is_on_floor():
			animation = "walking"
	else:
		velocity.x = move_toward(velocity.x, 0, 16)
		if is_on_floor():
			animation = "idle"  

func _jump(delta):
	if is_on_floor() and Input.is_action_just_pressed("Jump"):
		velocity.y = jump_velocity
		animation = 'jumping'
	elif not is_on_floor():
		_jetpacking(delta)

func _running():
	if Input.is_action_pressed('Sprint') and is_on_floor():
		speed = 280
	else:
		speed = 200

func _flip():
	if Input.is_action_just_pressed('GoLeft'):
		sprite_2d.flip_h = true
	if Input.is_action_just_pressed('GoRight'):
		sprite_2d.flip_h = false
#Function for moving end

func _physics_process(delta):
	#movements
	
	_jump(delta)
	_walking_n_running()
	_running()
	_flip()
	move_and_slide()
	
	#labelsetc_
	
	sprite_2d.animation = animation
	fuel_label.text = str(int(jetpack_fuel))
	health_label.text = str(int(health))
	
	#healthchanges
	damage(delta)

Can you be more explicit? What do you mean by add guns? do you want a shooting mechanic?

1 Like

I want to add this gun to the player as a sprite and as the child of the main and I want it to be able to shoot a 2 pixel long bullet and also moves up and down like the cursor
Képernyőkép 2024-08-19 225413

Képernyőkép 2024-07-26 173809

What were you struggling with specifically? Do you have a script to show, even in an incomplete state?

I used a variation of this for my helicopter game demo - you can watch it in action on my YouTube channel or play the demo on my itch.io page.

https://kidscancode.org/godot_recipes/4.x/2d/2d_shooting/

Let me know if you need more details. :smile: