Hey,
i am a completely new to Godot and after following the Tutorial (Dodge the Creeps) i've tried to adjust the Code to make something myself. Currently i have a CharacterBody2D that can move around and Mobs that get spawned along a path like it was done in the tutorial. But they are RigidBody2D now. I've made them so, that they follow me around by getting the players position. For this i had to get the player object in a variable in the mob.gd. But i am really confused now, because it worked earlier, but after i changed something in the way, the mobs follow me it does not work anymore, but i did not change something in the way i get the node?
This is the error:
E 0:00:01:0023 mob.gd:8 @ _ready(): Node not found: "../CharacterBody2D" (relative to "/root/Main").
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1364 @ get_node()
<Stack Trace> mob.gd:8 @ _ready()
main.gd:63 @ _on_mob_timer_timeout()
This is my Mob.gd:
extends RigidBody2D
var followSpeed: float = 1000.0
var player: CharacterBody2D
var nextToPlayer = false
# Called when the node enters the scene tree for the first time.
func _ready():
player = get_parent().get_node("../CharacterBody2D")
get_node("AnimatedSprite2D").play("default")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var direction = (player.global_position - global_position).normalized()
if player and !nextToPlayer:
linear_velocity = direction * followSpeed * delta
else:
linear_velocity = -direction * followSpeed * delta
pass
func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()
func _on_area_2d_body_entered(body):
if body.is_in_group("Player"):
nextToPlayer = true
func _on_area_2d_body_exited(body):
if body.is_in_group("Player"):
nextToPlayer = false
And here is a Picture of the main tree (hope this is displayed right).