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
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?