NPC AI doesn't follow the player

Godot Version

4.2.1

Question

I have made a simple for an enemy that follows the player and kills him. I copied the code for an NPC to follow him if the player interacted for a second time. but for some reason the NPC doesnt follow the player and kinda wonders around the map for no reason. can someone help?

here is the code:

extends CharacterBody2D
class_name NPC
var here=false
var string="res://Portrait/null.dtl"
var once=true
var choice
var follow=false
var speed=50
var cell=false
@onready var anim=$Victim
@onready var pcam=$"../player/PhantomCamera2D"
@onready var pcam2=$"../player/PhantomCamera2D2"
@onready var lantern=$"../player/Control2"
@onready var wall=$"../StaticBody2D"
@onready var targetplayer=$"../player/Control2"



func _physics_process(delta):
	if targetplayer==null and follow==false and cell==false: 
		targetplayer=get_tree().get_nodes_in_group("player")[0]
	if targetplayer!=null and follow==true and cell==true:
		velocity=position.direction_to(targetplayer.position)*speed
		move_and_slide() 
	if (targetplayer.position.x-position.x)>0:
		anim.flip_h=true
	else:
		anim.flip_h=false


func _on_watch_body_entered2(body):
	if body.name=="player":
		follow=false

func _on_watch_body_exited2(body):
	if body.name=="player":
		follow=true

func _input(event):
	if Input.is_action_just_pressed("ui_inter") and here==true and once==true:
		Dialogic.start(string)
		Dialogic.signal_event.connect(dia)
		Dialogic.timeline_ended.connect(ended)
		pcam2.set_priority(3)
		wall.set_collision_layer_value(3,true)
		here=false
		once=false
		cell=false
		lantern.hide()
	elif Input.is_action_just_pressed("ui_inter") and here==true and once==false:
		cell=true
		Dialogic.start("res://Timelines/cell.dtl")
		follow=true

Screenshot 2024-06-09 225059

he just walks past the player and ignoring his Code.

update: if I change the address of targetplayer to player itself, the NPC goes downstairs but if I change it to one of players childs, Like the control node, it walks down the hallway and then stops… can someone explain why the difference of the routes?
my understanding is that the address of player has a problem for some reason, so is there a code or an algorithm that doesnt require the address?

This line doesn’t do anything. Maybe assign it to the targetplayer variable?

targetplayer = get_tree().get_nodes_in_group("player")[0]

the player is part of a group called player… I know its not imaginative XD
Screenshot 2024-06-09 224218

That’s completely fine :grin: It’s a good name

But you are getting the nodes and you do nothing with them. I guess you want to assign the player node to some variable?

ok so you mean:
get_tree( targetplayer).get_nodes_in_group("player")[0]
or
get_tree().get_nodes_in_group( targetplayer)[0]

I am a bit novice in coding sorry

Read my first response :smiley: I mean this:

func _physics_process(delta):
	if targetplayer==null and follow==false and cell==false: 
		targetplayer =get_tree().get_nodes_in_group("player")[0]
	if targetplayer!=null and follow==true and cell==true:
		velocity=position.direction_to(targetplayer.position)*speed
		move_and_slide() 
	if (target.position.x-position.x)>0:
		anim.flip_h=true
	else:
		anim.flip_h=false

still nothing happens, the NPC goes somewhere else

Also take the direction from their global_position difference. The normal position is relative to the respective parent nodes, which is probably not what you want. One coul be a child of some NPCs container and player is at the top level and their positions relative to their parents may be completely different.

velocity = global_position.direction_to(targetplayer.global_position) * speed
move_and_slide()

so basically, instead of going to the hallway and standing there, like before he goes inside another room and collides with a wall. this happens if I give targetplayer address as the player itself

ok I found out what the solution is. the code is fine and good. the problem was the NPC scene. the sprite wasnt at the center.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.