0 votes

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

please open in new tab if not shown properly

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()
Godot version ver3.3.3
in Engine by (80 points)

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:"

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.