Can't get global position

Hei/Hello!

Your full script in triple quote thingies: ( ``` ← These things.)

extends CharacterBody2D

@onready var player = get_parent().get_parent().get_node(“Player”) 

var target = player.global_position

@onready var speed = 300

func _physics_process(delta: float) → void:
	var direction = (target - position).normalized()
	velocity = direction * speed * delta
	look_at(target)
	move_and_slide()

There are a few things that could be improved.


This…

@onready var player = get_parent().get_parent().get_node(“Player”) 

…could be changed to an export var like this:

@export var player: Object = null # P.S. if the player has a class_name, you can use "var player: some_name = null"

And then, in the enemy’s inspector you select the player (or other possible targets).


You should also move the “target” variable into the function, so it updates every time it is needed…

var target = player.global_position # This will only update once.

@onready var speed = 300

func _physics_process(delta: float) → void:
	var direction = (target - position).normalized()
	velocity = direction * speed * delta
	look_at(target)
	move_and_slide()

…like this:

@onready var speed = 300

func _physics_process(delta: float) → void:
	var target = player.global_position # Updates before moving.
	var direction = (target - position).normalized()
	velocity = direction * speed * delta
	look_at(target)
	move_and_slide()

Complete code:

extends CharacterBody2D

@export var player: Object = null # Set in inspector

@onready var speed = 300

func _physics_process(delta: float) → void:
	var target = player.global_position # Updates before moving.
	var direction = (target - position).normalized()
	velocity = direction * speed * delta
	look_at(target)
	move_and_slide()

Hope this helps! (Sorry if it’s a bit confusing. I struggle with terminology/vocabulary) (This code should work, but I haven’t tested it… I apologize in advance for any stupid mistakes I may have made)


Bonus: Using an autoload to share a reference:

If you plan on having multiple enemies, it could get tedious to update the export variable for every enemy, so I would suggest setting up an autoload (global script), that stores a reference to the player:

extends Node # This is the autoload script.

var player_ref: some_name = null # I also suggest giving the player a "class_name"

And then, before anything happens, the player sends itself to the autoload:

class_name Player  # Example player script:
extends CharacterBody2D

func _init(): # Init activates before most things.
	your_autoload_name.player_ref = self

And lastly, in your enemy:

extends CharacterBody2D

@onready var player: Player = your_autoload_name.player_ref # Onready var goes after init()

# Rest of the code...

This way, you don’t have to set up export variables AND it will also make instanced enemies to automatically know what their target is!

This is how I learned to do it after struggling with references for too long… (I couldn’t find resources to help me out, resources that I understood at least. So I’m leaving this here for anyone trying to get into coding!)