• 3D
  • I'm having trouble making an enemy AI follow the player

Hello, I'm currently following an enemy AI tutorial at [

] and I can't get the enemy to follow my character. The problem occurs at roughly 13:44 time in the video.

I'm altering the tutorial with the 3D mannequin tutorial I have already completed from the GD quest website, but I don't know if that matters or not. So instead of using the first person character controller that is demonstrated in the video, I am using the 3d mannequin.

My current code for the Enemy.gd script is as below: (excluding the lines below and beneath it)


extends KinematicBody

var target

func body_entered(body):
	target = body

func _physics_process(delta):
	if target:
		look_at(target.global_transform.origin, Vector3.UP)

func _on_Area_body_entered(body):
		print(body.name + " entered")
		set_color_red()
		if body.is_in_group("Player"):
			target = body

func _on_Area_body_exited(body):
		print(body.name + " exited")
		set_color_green()
		if body.is_in_group("Player"):
			target = null


	

func set_color_red():
	$MeshInstance.get_surface_material(0).set_albedo(Color(1, 0, 0))

func set_color_green():
	$MeshInstance.get_surface_material(0).set_albedo(Color(0, 1, 0))

I have been trying everything I can and don't know why this isn't working, as it works fine for the person in the video. Please help.

Welcome to the forums @LoganM!

Have you connected the body_entered and body_exited signals to the _on_Area_body_entered and _on_Area_body_exited functions? You can connect the signals in code using the following:

func _ready():
	# assuming the Area node is a child of this node...
	get_node("Area").connect("body_entered", self, "on_Area_body_entered")
	get_node("Area").connect("body_exited", self, "on_Area_body_exited")

Thanks so much for the response @TwistedTwigleg . I have tried adding the few lines of code into my current code, and I can't figure out a way to do so. The Area node is a child of the Enemy Kinematic Body node, as is seen in the below image, and the Area node also appears to have signals linking it to the body entered and exited nodes. Is there a specific way I should add the code?

Okay, well it looks like the signals are connected. Have you tested to see if the AI changes color when the player (or whatever other object) collides with it?

Also, in the code presented, I do not see anything for moving the AI. There only appears to be code for handling collisions with the player, but not actually moving the AI using something like move_and_collide or move_and_slide. Does a different script handle moving the AI, or maybe the code still needs to be added?

@TwistedTwigleg The point in the video that I am at it getting the enemy to just rotate (or look) at the player when it enters the enemy's line of sight. I haven't actually coded anything, such as move_and_slide , to make the enemy move and follow the player, as I haven't reached that part of the video yet. Also, when you enter the line of sight, the enemy turns red, and when you leave the line of sight the enemy goes back to green.