As the title states, My bullet follows the direction of my mouse, I'll provide the code I used.
Bullet code: (This handles in which direction and how fast my bullet will go)
func _physics_process(delta):
var direction = get_tree().root.get_child(0).get_node("Player").direction()
velocity = SPEED*delta*direction
$AnimatedSprite.rotation_degrees = get_tree().root.get_child(0).get_node("Player").get_child(1).get_child(4).rotation_degrees
translate(velocity)
Hand code: (This handles the rotation of the player's hand including the gun)
func _process(delta):
var mpos = get_global_mouse_position()
look_at(mpos)
if rotation_degrees <= -90:
rotation_degrees = -90
elif rotation_degrees >= 90:
rotation_degrees = 90
Bullet spawn code: (I think this is where the problem lies, as it handles where the bullet will spawn)
func _process(delta):
if Input.is_action_pressed("Shoot"):
shoot()
func shoot():
if $Cooldown.is_stopped():
var bullet = BULLET.instance()
get_tree().root.get_node("Stage").add_child(bullet)
bullet.global_position = $Body/Sprite/Position2D.global_position
$Cooldown.start()
func direction():
var direction = (get_global_mouse_position() - $Body/Sprite/Position2D.global_position).normalized()
return direction
