Below involved code. I've reviewed many times but I don´t find the mistake.
Its a kind of state machine, where states change according timers or triggering areas. I start/stop them when useless as I'm trying to get a mobile acceptable performance and many timers, -all of them set to Physics-, had impact.
Note that last function is triggered always before actual error function with no issue.
func _on_Update_path_timer_timeout():
path_index = 0
if player.vida == 0:
estado_actual = estados.PATROL
if estado_actual == estados.PERSEGUIR:
update_path(player.global_transform.origin)
elif estado_actual == estados.PATROL:
if !actual_point ==null:
update_path(actual_point.global_transform.origin)
else: return
func _on_Attack_timer_timeout():
puede_atacar = true
func _on_Area_ataque_body_entered(body):
if body == player:
Update_path_timer.stop()
$Attack_timer.start(randi()%2+1)
if !regen_timer.is_stopped():
regen_timer.stop()
heal_graphic.emitting = false
estado_actual = estados.ATACAR
func _on_Area_ataque_body_exited(body):
if body == player and estado_actual != estados.RETROCEDER:
Update_path_timer.start()
_on_Update_path_timer_timeout()
estado_actual = estados.PERSEGUIR
func _on_Campo_vision_body_entered(body):
if body == player:
var dist_player = (player.global_transform.origin - global_transform.origin).normalized()*2
var muro = ray.intersect_ray(global_transform.origin+Vector3(0,1.5,0),
player.global_transform.origin+Vector3(0,1.5,0)+dist_player, [self])
if !muro.collider.is_class("KinematicBody"):
return
_on_Update_path_timer_timeout()
Update_path_timer.start()
$Campo_vision/CollisionPolygon.disabled = true
estado_actual = estados.PERSEGUIR