0 votes

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 physicsprocess(delta: float) -> void:
if player != null:
move = position.directionto(player.position) * speed
move
and_collide(move)

func onPlayerckeckbodyentered(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 = enemypreload.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 !

Godot version 3.3
in Engine by (46 points)

1 Answer

0 votes

"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 https://docs.godotengine.org/en/stable/classes/class_node2d.html

by (14 points)

Thanks alot ! it works well

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.