How to code so that "If i look at enemy, it doesn't move but if i don't look at it, i moves"

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

Here is my enemy code.
Enemy Stops but doesn’t move again:

extends KinematicBody

export var SPEED = 3
var target = null
var nav: Navigation = null
var vel = Vector3()

func _physics_process(delta):
	
	if target == null:
		return
	
	look_at(target.global_transform.origin, Vector3.UP)
	
	var path = get_to_path(target.global_transform.origin)
	if path.size() > 0:
		move_along_path(path)

func get_to_path(target):
	return nav.get_simple_path(global_transform.origin, target)

func move_along_path(path):
	if path.size() <= 0:
		return
	
	path.remove(0)
	
	var target = path[0]
	
	if global_transform.origin.distance_to(target) < 0.1:
		path.remove(0)
	
	vel = (target - translation).normalized() * SPEED
	
	vel = move_and_slide(vel)

func set_target(target):
	self.target = target

func set_nav(nav):
	self.nav = nav

func stop():
	SPEED = 0

func move():
	SPEED = 3


func _on_Area_area_entered(area):
	if area.is_in_group("STOPPER"):
		stop()
	else:
		vel = (target - translation).normalized() * 3extends KinematicBody

export var SPEED = 3
var target = null
var nav: Navigation = null
var vel = Vector3()

func _physics_process(delta):
	
	if target == null:
		return
	
	look_at(target.global_transform.origin, Vector3.UP)
	
	var path = get_to_path(target.global_transform.origin)
	if path.size() > 0:
		move_along_path(path)

func get_to_path(target):
	return nav.get_simple_path(global_transform.origin, target)

func move_along_path(path):
	if path.size() <= 0:
		return
	
	path.remove(0)
	
	var target = path[0]
	
	if global_transform.origin.distance_to(target) < 0.1:
		path.remove(0)
	
	vel = (target - translation).normalized() * SPEED
	
	vel = move_and_slide(vel)

func set_target(target):
	self.target = target

func set_nav(nav):
	self.nav = nav

func stop():
	SPEED = 0

func move():
	SPEED = 3


func _on_Area_area_entered(area):
	if area.is_in_group("STOPPER"):
		stop()
	else:
		vel = (target - translation).normalized() * 3
:bust_in_silhouette: Reply From: Gluon

What you want is a boolean guard. So where you have

func _on_Area_area_entered(area):
    if area.is_in_group("STOPPER"):
        stop()
    else:
        vel = (target - translation).normalized() * 3

what you want is instead to have a variable boolean which is true when it can move and false when it cannot move. The when it enters the area set it to false and have code in your _process() function to stop movement then attach a signal for body exited to this script from the area2d to turn the boolean to true again. What is happening at the moment is it is stopping but never seeing anything to tell it to move again.

I just added this and it works!!!

on the top I did

var can_move = true

then i added this

func _on_Area_area_entered(area):
	if area.is_in_group("STOPPER"):
		can_move = false


func _on_Area_area_exited(area):
		if area.is_in_group("STOPPER"):
			can_move = true

func _process(delta):
	if can_move == true:
		SPEED = 3
	elif can_move == false:
		SPEED = 0

iAbrees | 2022-12-26 14:01

Perfect glad it all worked for you!

Gluon | 2022-12-26 14:02