What ways are there to get the Player node's position on enemy nodes?

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

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

:bust_in_silhouette: Reply From: sparkart

You can use get_node() to access the player’s position. Take a look at the documentation for more information about it: Node — Godot Engine (3.1) documentation in English

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

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.

KijeviGombooc | 2019-07-16 18:26

2 Likes
:bust_in_silhouette: Reply From: KijeviGombooc

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.

:bust_in_silhouette: Reply From: k3nzngtn
export (NodePath) var playerNodePath

func _physics_process(delta):
   var player = get_node(playerNodePath)