Godot Version
extends CharacterBody2D
var target = get_parent().get_node(“Player/plrimg”)
var speed = 200
func _process(delta: float) → void:
var direction = (target.position - position).normalized()
velocity = direction * speed
look_at(target.position)
move_and_slide()
Question
im trying to make the enemy follow the player but "get_node on null value
you can try
@onready var target = ...
you should also use “_physics_process” instead of “_process”
1 Like
Does it still have the same error? Then it means your node path to the player image is wrong. Right now the code is written for a player that is the sibling of the enemy.
prefer using groups to find the player. put the player on a unique group and call get_nodes_in_group.
the way you are doing it now will break if the player is accidentally or purposefully placed in a different path.
you are also calling a node inside player which is a big no no. put a method in player and call that to get what you need, like a transformed position or the position of a child node, or a child node. the way you are doing it can also break very easily so it prevents your scenes from being modified.
you are going towards position. position is local to the node and node parent.
use global_position instead.
look_at(target.global_position)
1 Like