Godot Version
Godot 4.3
Question
i’m making a 2D platformer and added a gun,bullet,player,main scene but if i shoot,the bullet always goes to the right.and i want that if like the players looks to the left and shoots that the bullet goes to the left and if the player makes a double jump that a bullet will be shooted and go down.btw here is the code of my scenes:
player:
extends CharacterBody2D
const SPEED = 250.0
const JUMP_VELOCITY = -600.0
var jump_max = 2
var jump_counter = 0
@onready var gun: Node2D = $Gun
@onready var anim: AnimatedSprite2D = $AnimatedSprite2D
func _ready() → void:
pass
func _physics_process(delta: float) → void:
if Input.is_action_just_pressed("attack"):
gun.shoot()
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if is_on_floor() and jump_counter != 0:
jump_counter = 0
if jump_counter < jump_max:
if Input.is_action_just_pressed("jump"):
velocity.y = JUMP_VELOCITY
jump_counter += 1
# 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("left", "right")
if Input.is_action_pressed("left"):
anim.flip_h = true
if Input.is_action_pressed("right"):
anim.flip_h = false
else:
if is_on_floor():
if direction > 0:
anim.play("run")
if direction < 0:
anim.play("run")
if direction == 0:
anim.play("idle")
else:
anim.play("jump")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
bullet:
extends Area2D
@export var speed = 600
@export var damage = 15
var direction:Vector2
func _ready() → void:
await get_tree().create_timer(3).timeout
queue_free()
func set_direction(bulletdirection):
direction = bulletdirection
rotation_degrees = rad_to_deg(global_position.angle_to_point(global_position+direction))
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _physics_process(delta: float) → void:
global_position += direction * speed * delta
func _on_body_entered(body: Node2D) → void:
if body.is_in_group(“Enemies”):
body.get_damage(damage)
queue_free()
gun:
extends Node2D
@export var shootSpeed = 10.0
@onready var marker_2d: Marker2D = $Marker2D
@onready var shoot_speed_timer: Timer = $ShootSpeedTimer
const BULLET = preload(“res://scenes/bullet.tscn”)
var can_shoot = true
var bulletDirection = Vector2(1, 0)
Called when the node enters the scene tree for the first time.
func _ready() → void:
shoot_speed_timer.wait_time = 1.0 / shootSpeed
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func shoot() → void:
if can_shoot:
can_shoot = 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() → void:
can_shoot = true
func setup_direction(direction):
bulletDirection = direction
if direction.x > 0:
scale.x = 1
rotation_degrees = 0
elif direction.x < 0:
scale.x = -1
rotation_degrees = 0
elif direction < 0:
scale.x = 1
rotation_degrees = -90
elif direction.y > 0:
scale.x = 1
rotation_degrees = 90
if you know what i have to do,pls reply i will really appreciate it