How to only shoot a bullet at right side of Position2d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By amaou310

Right now, I can shoot my bullet from position2d which is positioned at tip of the gun. The bullet will be launched at left side even the gun sprite is looking at right. I only want my gun to be able to shoot the bullet when the mouse is right side of the position2d.
How could I do that ?

Gun Script

extends Position2D



export (PackedScene) var bullet_scene := preload("res://Scenes/Bullet01.tscn")
onready var cooldownTimer = get_node("../cooldownTimer")


func _process(delta: float) -> void:
	if Input.is_action_pressed("shoot") and cooldownTimer.is_stopped():
		shoot()

func shoot():
	var bullet = bullet_scene.instance()
	get_parent().add_child(bullet)
	bullet.global_position = self.global_position
	bullet.direction = (get_global_mouse_position() - global_position).normalized()
	bullet.rotation = bullet.direction.angle()
	cooldownTimer.start()

Bullet Script

   extends Area2D
        
        var speed := 700
        var max_range := 300.0
        var _travelled_distance = 0.0
        var damage := 90
        
        var direction = Vector2.ZERO
        
        func _init():
        	set_as_toplevel(true)
        
        func _physics_process(delta: float) -> void:
        	var distance := speed * delta
        	var motion := transform.x * speed * delta
        	
        	position += motion
        	
        	_travelled_distance += distance
        	if _travelled_distance > max_range:
        		queue_free()
        
        
        func _on_VisibilityNotifier2D_screen_exited():
        	queue_free()
        	print("deleted")
:bust_in_silhouette: Reply From: ramazan
func _process(delta: float) -> void:
    if get_global_mouse_position().x > global_position.x:
        flip_h = true
    else:
        flip_h = false
          
    if Input.is_action_pressed("shoot") and cooldownTimer.is_stopped():
        shoot()

Thank you ramazan! This will only work when the parent sprite is looking at right. But not on left side. How about when the sprite flip?

amaou310 | 2022-08-14 13:30

I fixed it. try again

ramazan | 2022-08-14 13:54

Thank you again!
I flip my player sprite by changing the scale of position2d which is a parent of “PlayerSkinIK” scene. “Player” and “PlayerSkinIK” are two different scene and I instance “PlayerSkinIK” scene as a child of “Player” Scene. I thought I could restrict my shooting direction by simply adding if position2d.scale.x == 1 inside the script attached to GUN sprite. However, I cannot do onready var position2d = get_node("Node Path of Position2D") because Position2D is a different node inside “Player” Scene, and not in “PlayerSkinIK” Scene.

Its like this:

amaou310 | 2022-08-15 02:45

Okay, never mind. I used direction instead. but it still only work right side.

if Input.is_action_pressed("shoot") and cooldownTimer.is_stopped():
		if direction == 1:
			if get_global_mouse_position().x > global_position.x:
				shoot()
		if direction == -1:
			if get_global_mouse_position().x < global_position.x:
				shoot()

amaou310 | 2022-08-16 07:37