Please note that my Godot version is 3.3 so that could be a reason.
I tried to create a script to shoot bullets.
First I tried using the input function
func _input(event):
# Mouse in viewport coordinates.
if event is InputEventMouseMotion:
#print("Mouse Motion at: ", event.position)
look_at(event.position)
if event is InputEventMouseKey:
direction = event.position
print("Mouse Click/Unclick at: ", get_global_mouse_position())
var shoot = bullet.instance()
get_parent().add_child(shoot)
shoot.global_position = global_position
yield(get_tree().create_timer(0.08), "timeout")
Then I tried using event.isEcho()
and a state (also used Input.is_just_pressed()
)
enum {
IDLE
SHOOTING
}
Here is my current code: Sprite code (player/gun)
extends Sprite
onready var bullet = preload("res://bullet.tscn")
onready var direction = null
var pos = null
# Called when the node enters the scene tree for the first time.
func _ready():
pass
func _process(delta):
if Input.is_action_just_pressed("click"):
direction = get_global_mouse_position()
print("Mouse Click/Unclick at: ", get_global_mouse_position())
var shoot = bullet.instance()
get_parent().add_child(shoot)
shoot.global_position = global_position
yield(get_tree().create_timer(0.08), "timeout")
func _input(event):
# Mouse in viewport coordinates.
if event is InputEventMouseMotion:
#print("Mouse Motion at: ", event.position)
look_at(event.position)
Bullet: (not so relevant as my question is why process and input runs twice)
extends KinematicBody2D
var direction = Vector2.ZERO
var MAX_SPEED = 20
var ACCELERATION = 5
var velocity = Vector2.ZERO
onready var tween = $Tween
func _ready():
direction = mouse.direction
# (81, 23)
tween.interpolate_property(self, 'position', position, direction, 0.8, Tween.TRANS_LINEAR, Tween.EASE_OUT)
tween.start()
yield(get_tree().create_timer(0.8), "timeout")
queue_free()
I changed the code to:
func _process(delta):
if Input.is_action_pressed("click"):
set_process(false)
direction = get_global_mouse_position()
print("Mouse Click/Unclick at: ", get_global_mouse_position())
var shoot = bullet.instance()
get_parent().add_child(shoot)
shoot.global_position = global_position
yield(get_tree().create_timer(0.08), "timeout")
set_process(true)
Still doesn't work.
The _input is run exactly at the same time so that's probably why.
Log:
Mouse Click/Unclick at: (453, 177)
Mouse Click/Unclick at: (453, 177)
Result: One bullet comes from (0, 0) going to some random place (I think), and another goes to the right position.
Sprite 2 is the gun. sprite is the player character.
After one click