Enemy Shooting at Projectile Help

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

My intention is to have a projectile spawn upon player entering an Area 2D, and travel along the line from origin to the Player’s position when it spawns, without stopping until it collides with the player or a world feature.

Instead, the projectile always aims towards the (+x, +y) coordinates and continues until it collides. It does not ever move towards -x or -y.

I am hoping someone can help me understand 1. why the code below lead to the variant behavior I’m seeing in the image, and 2. what I could change in order to achieve my desired functionality.

extends KinematicBody2D

onready var MAXSPEED = 60
onready var player_Position = Vector2.ZERO
onready var direction = Vector2.ZERO
onready var origin = self.global_position

func _ready():
	player_Position = get_node("/root/Arena/YSort/Player").get_global_position()
	direction = (player_Position - origin).normalized()
	
func _process(delta):
	move_and_collide(direction * delta * MAXSPEED)

Image

:bust_in_silhouette: Reply From: Mycenae1600

From /u/Grulps - the solution was that _ready was called before setting the position in the scene that spawned each ‘bullet’.

Setting the global position of the instance before add_child worked:

ringBullet.global_position = bulletSpawn + bulletRandom
get_node("/root/Arena/YSort").add_child(ringBullet)