How to make randomly generated enemies chase the player?

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

I am making the 2D game from FreeCodeCamps crash course on Godot. I have created a frog as an enemy and added it to the world, and the frog chases the player in whichever direction the player is as it is supposed to. However, when I randomly spawn the frog enemies, they don’t chase the player and only go to right when they detect the player. How to make them change direction according to the position of the player?

#code to randomly spawn frog:

func _on_frog_timer_timeout():
	var frogTemp=Frog.instantiate()
	var rng2=RandomNumberGenerator.new()
	var ranint2=rng2.randi_range(0,2500)
	frogTemp.position=Vector2(ranint2,350)
	add_child(frogTemp)

#code for frog detection, collision and death

extends CharacterBody2D

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var player
var SPEED=100
var chase=false

func _ready():
	get_node("AnimatedSprite2D").play("Idle")

func _physics_process(delta):
	velocity.y += gravity * delta
	if chase==true:
		if get_node("AnimatedSprite2D").animation !="Death":
			get_node("AnimatedSprite2D").play("Jump")
		player=get_node("../../Player/Player")
		var direction=(player.position-self.position).normalized()
		if direction.x>0:
			get_node("AnimatedSprite2D").flip_h= true
		else:
			get_node("AnimatedSprite2D").flip_h= false
		velocity.x=direction.x*SPEED
		move_and_slide()
	else:
		if get_node("AnimatedSprite2D").animation !="Death":
			get_node("AnimatedSprite2D").play("Idle")
		velocity.x=0

func _on_p_layer_detection_body_entered(body):
	if body.name=="Player":
		chase=true

func _on_p_layer_detection_body_exited(body):
	if body.name=="Player":
		chase=false
	

func _on_player_death_body_entered(body):
	if body.name=="Player":
		death ()

func _on_player_collision_body_entered(body):
	if body.name=="Player":
		Game.PlayerHP-=3
		death()
		
func death():
	Game.Gold+=5
	Utils.saveGame()
	chase=false
	get_node("AnimatedSprite2D").play("Death")
	await get_node("AnimatedSprite2D").animation_finished
	self.queue_free()

Edited to format the code. Use the Code Sample button.

Zylann | 2023-07-06 09:37

I don’t know the reference off hand but it’s always worth comparing the code to verify it is as expected; seems like it works fine from your description, except for the “randomly spawned”?

My guess would be the player reference is invalid:

player=get_node("../../Player/Player")

Because if the spawned frogs aren’t in the same place in the SceneTree, they won’t get a reference (also, godot should be producing errors - what’s in the debugger?). Where are the newly spawned frogs being spawned?

spaceyjase | 2023-07-06 11:34

the player reference and the frogs seem to work fine when I place them manually by dragging and dropping into my world scene. These frogs will chase the player, change directions and stop chasing if the player leaves the detection zone. It is only the randomly generated frogs which wont change direction. They spawn randomly within the given limits as they should and they also start chasing the player only when the player enters the detection zone. However they dont change directions to chase the player, they just keep going to the right. This problem does not occur with the manually placed frogs. I would assume the manually placed frogs and the randomly spawned frogs both use the same script (I am totally new to godot so idk for sure).

As for the errors part, there are no errors the game seems to be working just fine except the randomly generated frogs which wont turn towards the player

Rnt | 2023-07-06 19:53