I am currently working on a side-scrolling action game as of now and for a while, I have attempted to implement a gun in said game
so essentially, the way i want my gun to work is like this:
It’s really straightforward, the gun moves left and right in accordance to the player’s current direction, be unequipped any time, and have an ammo system
I have attempted at implementing the gun various times, although none of my attempts really worked, as I am still relatively new to GDScript.
and also, here’s my complete CharacterBody2D script as a reference (I have ripped some of the GDScript code from the official 2D Platformer Demo although I have made a half-baked implementation of the gun code from that):
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
@export var action_suffix := ""
var gun = get.node(gun)
# HALF LIFE 3 CONFIRMED
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
var is_shooting := false
if Input.is_action_just_pressed("shoot" + action_suffix):
is_shooting = gun.shoot()
move_and_slide()
func _input(_event):
if Input.is_action_just_pressed("ui_cancel"):
get_tree().quit()
print ("bye bye bitch!");
honestly, any help, suggestions, or advice would be helpful and pretty much welcome
I’ve done some of that in my 2D Helicopter demo, and will am happy to send you any code or bits you need.
I used a marker2D to attach the cannon to the front of the helicopter and created a separate scene for the rocket - rocket.tscn - as once the rocket leaves the player it needs to behave independently.