Attention | Topic was automatically imported from the old Question2Answer platform. | |
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")