How to prevent bullet instance inner side of position2d

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

Hello Community, I would appreciate if you could teach me how to prevent bullet instancing inner side of position2D.

In my player script attached to player node, I flip player sprite.

func _process(delta: float) -> void:
	if get_global_mouse_position().x > $Position2D/PlayerSkinIK.global_position.x: #flip player sprite
		position2D.scale.x=1
	else:
		position2D.scale.x=-1

In gun sprite script attached to gun sprite node, I rotate gun toward mouse position

func _process(delta):
	look_at(get_global_mouse_position())

In gun script attached to position2d node, I instantiate bullet node

extends Position2D

export (PackedScene) var bullet_scene := preload("res://Scenes/Bullet02.tscn")
onready var cooldownTimer = get_node("../cooldownTimer")
onready var _animation_player = get_node("../../AnimationPlayer")
onready var position2d = get_node("../Player/Position2D")
onready var shootsound = get_node("../../ShootSound")

var shoot = true
var direction = 1

func _process(delta: float) -> void:

	if shoot == true:
		if Input.is_action_pressed("shoot") and cooldownTimer.is_stopped():
			shoot()
			shootsound.play()  

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


func _on_Player_player_in_hitbox_false():
	shoot = false #Disable Shooting

func _on_Player_player_in_hitbox_true():
	shoot = true #Enable Shooting

I tried to solve the problem by following code, yet this will only prevent bullet instance inner side of position2d when player is looking at right side. when player is looking at left side, bullet will not be instanced.

func _process(delta: float) -> void:

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

A Position2D is just a set of co-ords, so I’m not sure what you mean by an “inner side”?

Also, the code inside the “if direction == -1:” line does exactly the same as the code for “if direction == 1:”

SteveSmith | 2023-01-27 10:12