I made a program for enemy to follow and kill the player but it doesn't working

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

I need help :
i made a program for enemy to follow and kill the player but it doesn’t working

extends KinematicBody2D

var speed = 300
onready var obj = get_parent().get_node(“Player”)

func _physics_process(delta):
var dir = (obj.global_position - global_position).normalized()
move_and_collide(dir * speed * delta)

func _on_Area2D_body_entered(body):
if body.get_name() == “Player”:

:bust_in_silhouette: Reply From: Tomi

What is the problem exactly? E.g. the enemy is moving but not collide with the player?
Have you try this?

var playerobj=move_and_collide(dir * speed * delta)
if (playerobj):
if playerobj.get_collider()==“Player”:

:bust_in_silhouette: Reply From: timothybrentwood


func _physics_process(delta):
    var obj = getparent().getnode("Player")
    if obj:
        var dir = (obj.globalposition - globalposition).normalized()
        moveandcollide(dir * speed * delta)

If that works it’s because your Player node wasn’t _ready() at the time that object was _ready().

“When instantiating a scene connected to the first executed scene, Godot will instantiate nodes down the tree (making _init calls) and build the tree going downwards from the root. This causes _enter_tree calls to cascade down the tree. Once the tree is complete, leaf nodes call _ready. A node will call this method once all child nodes have finished calling theirs. This then causes a reverse cascade going up back to the tree’s root.”