Enemies from spawner dont act as enemies directly from level

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

Hi ! I’m trying to make an enemy spawner.
The enemy is supposed to chase the player if it enters a zone. If i place the enemy directly in my level scene, it works well. But when the enemy if from the spawner, it dosent see the player…

here is the script for my enemy chasing the player:

func _physics_process(delta: float) → void:
if player != null:
move = position.direction_to(player.position) * speed
move_and_collide(move)

func _on_Playerckeck_body_entered(body: Node) → void:
if body != self:
player = body
print(body)

here is my script for the spawn:
extends Node2D

var enemy_preload = load(“res://actors/enemy.tscn”)

func _ready() → void:
spawn()
pass

func spawn():
var enemy = enemy_preload.instance()
get_node(“container”).add_child(enemy)

My spawn is composed of two nodes, one parent node that is “spawner” and a child node that is ‘container’.

thanks a lot !

:bust_in_silhouette: Reply From: StormhavenDev

“position” on a Node2D is relative to the parent’s position. So, for the enemy’s movement direction, you’re currently getting a vector from the enemy’s position local to its parent to the player’s position local to its parent.

To fix this, you probably just have to use the global_position instead of position with both the enemy and the player.

For more details, check out the properties of Node2D at Node2D — Godot Engine (stable) documentation in English

Thanks alot ! it works well

dany_pitre | 2021-05-03 15:15