How to make my bullet face to the right and left without the mouse pointer

Godot Version

4.3

Question

im trying to make a metroidvania(platformer) and when is shoot the bullet only goes to the right.does anyone know how to fix it.btw here is my code:

###Player

extends CharacterBody2D

@onready var anim: AnimatedSprite2D = $AnimatedSprite2D
@onready var gun: Node2D = $Gun

@export var SPEED : int = 300
var JUMP_VELOCITY : int
@export var jump_max : int

var jump_counter : int = 0

func _ready() → void:
jump_counter = jump_max

func _physics_process(delta: float) → void:
top_level = true

if Input.is_action_just_pressed("attack"):
	gun.shoot()

# Handle jump.
if Input.is_action_just_pressed("jump"):
	jump_counter += 1
	
	if jump_counter <= jump_max:
		if jump_counter == 2:
			JUMP_VELOCITY = -500
			velocity.y = JUMP_VELOCITY
		else:
			JUMP_VELOCITY = -400
			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("left", "right")

if Input.is_action_just_pressed("left"):
	anim.flip_h = true
if Input.is_action_just_pressed("right"):
	anim.flip_h = false
	
if is_on_floor():
	jump_counter = 0
	if direction > 0 or direction < 0:
		anim.play("run")
	if direction == 0:
		anim.play("idle")
else:
	velocity += get_gravity() * delta
	if jump_counter != 2:
		if velocity.y < 0:
			anim.play("jump")
		if velocity.y == 1:
			anim.play("mid-air")
		if velocity.y > 0:
			anim.play("falling")
		
	if is_on_ceiling():
		jump_counter = jump_max

if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	

move_and_slide()

###Gun

extends Node2D

@export var shootSpeed : int = 8

const BULLET = preload(“res://scenes/bullet.tscn”)

@onready var marker_2d: Marker2D = $Marker2D
@onready var shoot_speed_timer: Timer = $shootSpeedTimer
@onready var sprite: Sprite2D = $Sprite2D
@onready var sprite2D: Sprite2D = $Sprite

var canShoot : bool = true
var bulletDirection = Vector2(1,0)

func _ready() → void:
shoot_speed_timer.wait_time = 1.0 / 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() → void:
canShoot = true

func _physics_process(delta: float) → void:
if Input.is_action_just_pressed(“left”):
sprite.hide()
sprite2D.show()
if Input.is_action_just_pressed(“right”):
sprite.show()
sprite2D.hide()

###Bullet

extends Area2D

@export var speed : int = 800
@export var damage : int = 10

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))

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()
pass

if you know the answer pls reply.I will really appreciate it :grinning:

Your bulletDirection variable in the Gun script is set to point to the right always, I don’t see any place where you would change it, so it’s always gonna be going to the right.
bulletDirection = Vector2(1,0)

1 Like

it worked but the marker 2D is still the same do you know wich code do ihave to do to move the marker to another position?THX

forget about it. i fixed it by myself btw THX for the first problem :smiley:

1 Like