I’m making a skeleton enemy according to the tutorial “Projectile and RayCast2D in Godot 4” by “16BitDev” that will track the player’s location with RayCast.
Code:
@onready var ray_cast: RayCast2D = $RayCast
var has_a_bomb : bool = true
var is_dead : bool = false
enum skeleton_type {WITH_BOMB, WITH_KNIFE}
@export var skeleton : skeleton_type
var player
func _ready() -> void:
player = get_tree().get_root().get_node("Player")
func _physics_process(delta: float) -> void:
_aim()
_check_player_collision()
func _aim():
ray_cast.target_position = to_local(player.global_position) <-- here's problem
func _check_player_collision():
pass
Can you recheck that the player is actually exists in the scene in game? While the game is running (or shows error), at the top of the scene tree you will find a “remote” tab where you can observe every nodes in-game.
Not sure why its not working, last try from me is to first define a Global autoload and define player variable. In the player script, do Global.player = self, then do like this:
func _aim():
if Global.player: ray_cast.target_position = to_local(Global.player.global_position)