The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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 = getparent().getnode("Player")

func physicsprocess(delta):
var dir = (obj.globalposition - globalposition).normalized()
moveandcollide(dir * speed * delta)

func onArea2Dbodyentered(body):
if body.getname() == "Player":
get
tree().change_scene("res://TitleScreen/TitleScreen.tscn")

Godot version Godot v.3.3-stable
in Engine by (20 points)

2 Answers

0 votes

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

var playerobj=moveandcollide(dir * speed * delta)
if (playerobj):
if playerobj.getcollider()=="Player":
gettree().change
scene("res://TitleScreen/TitleScreen.tscn")

by (52 points)
0 votes

Try:

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 _entertree 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."

by (3,906 points)
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 Frequently asked questions and 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 [email protected] with your username.