0 votes

I want to access player's position on enemy nodes, so they can chase the payer. What good ways are there to do this?

in Engine by (146 points)

2 Answers

0 votes
Best answer

What I found to be useful, is adding all enemies to "ENEMIES" group, then make then have a method "add_player" and call this method of the group on player script.

by (146 points)
0 votes

You can use get_node() to access the player's position. Take a look at the documentation for more information about it: https://docs.godotengine.org/en/3.1/classes/class_node.html#class-node-method-get-node

Here is an example: get_node(“/root/player”).get_position()
You can also use $ instead for short-hand: $player.get_position();

by (331 points)

I feel like this is not good, because the relation of the player and the enemy nodes might chance overtime, so the sript(s) need change too. What I found to be useful, is adding all enemies to "ENEMIES" group, then make then have a method "add_player" and call this method of the group on player script.

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.