Player code:
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -300.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite = $AnimatedSprite2D
@onready var gun = $Gun
func _physics_process(delta):
#Shoot.
if Input.is_action_pressed("shoot"):
gun.shoot()
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
var direction = Input.get_axis("left", "right")
# Sprite flip.
if direction > 0:
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
Gun’s code
extends Node2D
@export var shootSpeed = 1.0
const BULLET = preload("res://scene/bullet.tscn")
@onready var marker_2d = $Marker2D
@onready var shoot_speed_timer = $shootSpeedTimer
var canShoot = true
var bulletDirection = Vector2(1, 0)
func _ready():
shoot_speed_timer.wait_time = 0.2 / shootSpeed
func shoot():
if canShoot:
canShoot = false
shoot_speed_timer.start()
var bulletNode = BULLET.instantiate()
bulletNode.set_direction(bulletDirection)
get_tree().root.add_child(bulletNode)
bulletNode.global_position = marker_2d.global_position
func _on_shoot_speed_timer_timeout():
canShoot = true
Surely, Im very very new to coding, I just learned HTML and CSS, and going into these type of coding just blew my mind. I searched all over youtube and trying like 4 different ways of shooting projectile, this one did the job but the creator built it for top-down 2d game, while I want to adapt it for sidescroller, platformer type of game. I think I need to make my player’s sprite to face my mouse to yet still have the ability to walk… backward? I dont even know what/how do you call that, any help will be appreciated, thank you.