Why does AI does not follow its dedicated path?

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

Hi!
I am currently working on a 2D bee game, where the players has to escape an enemy wasp. Before encountering the player, I want the wasp to patrol around the map. Therefore I have outlined a patrol_path using Path2D-Node. Yet upon starting the game, the wasp decides to fly another way (i guess to (0,0)).

I am following the tutorials by KidsCanCode.
https://kidscancode.org/godot_recipes/ai/path_follow/



extends KinematicBody2D

enum States {PATROL,ATTACK,CHASE}

var states=States.PATROL

export (NodePath) var patrol_path
var patrol_points
var patrol_index=0
export var patrol_speed=50

export var chase_speed = 100
var velocity=Vector2.ZERO 
var player=null



onready var animated_wasp: AnimationPlayer = get_node("AnimationPlayer")


func _ready():
	if patrol_path:
		patrol_points=get_node(patrol_path).curve.get_baked_points()

func _physics_process(delta):
	var velocity =Vector2.ZERO
	choose_action()


func choose_action():
	match states:
		
		States.PATROL:
			if !patrol_path:
				return
			var target = patrol_points[patrol_index]
			if position.distance_to(target)<1:
				patrol_index=wrapi(patrol_index+1,0,patrol_points.size())
				target=patrol_points[patrol_index]
			velocity=(target-position).normalized()*patrol_speed
			if velocity.x>0:
				animated_wasp.play("Wasp_Flight")
				$Wasp.set_flip_h(false)
			else:
				animated_wasp.play("Wasp_Flight")
				$Wasp.set_flip_h(true)
			velocity=move_and_slide(velocity)
			
			
		States.CHASE:
			if player:
				velocity=(player.position-position).normalized()*chase_speed
				
				if velocity.x>0:
					animated_wasp.play("Wasp_Flight")
					$Wasp.set_flip_h(false)
				else:
					animated_wasp.play("Wasp_Flight")
					$Wasp.set_flip_h(true)
			velocity=move_and_slide(velocity)
			
		States.ATTACK:
				pass
				
		
		
	pass


func _on_AttackRadius_body_entered(body):
	if body.get_name() =="Player":
		player=body
		states=States.ATTACK

func _on_AttackRadius_body_exited(body):
	if body.get_name()=="Player":
		states=States.CHASE

func _on_Detect_Radius_body_entered(body):
	if body.get_name()=="Player":
		player=body
		print("Playerentered")
		states=States.CHASE

func _on_Detect_Radius_body_exited(body):
	pass

func _on_Chase_Radius_body_exited(body):
	if body.get_name()=="Player":
		states=States.PATROL

print state and patrol_points, or pause game/breakpoint and check them in remote view.

Inces | 2022-09-17 18:40

:bust_in_silhouette: Reply From: aXu_AP

At quick glance logic seems to be good. One thing to note is that Path2D returns points in local space. If you have moved, rotated or scaled the Path2D node then points don’t match up with what you are seeing on screen.
You can adjust for this by transforming the points into global space:

var target_global = get_node(patrol_path).to_global(target)

Another possible pain point can emerge if wasp overshoots the target and ends up circling around it. If this does happen, raise the distance check to something like 10.

Thank you very much! Thats was exactly the point. I changed all target coordinates to global, so now the problem is solved.

TechNick | 2022-09-18 11:19

Glad to hear you could solve it! Don’t forget to mark the question solved, so someone with a similiar problem can find it faster :slight_smile:

aXu_AP | 2022-09-18 13:09