how to make Better AI pathfinding?

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

Hi actually, I try to improve my enemy AI of my 2D project but the promblem it I can’t make the enemy detect the wall and find a other way to go to the current position of the player just like this.

LEGEND:
square:wall
circle:enemy
diamond:player

I am thinking to do it whit Raycast2D but I don’t think it gonna work or whit ASart but I don’t not how to use it (~_~;).Thank for helping me it really appreciate.

here my code of my enemy.

extends KinematicBody2D

export (int) var max_health = 75
signal health_changed
export (int) var MOVE_SPEED
export(int) var detect_radius
export(int) var damage = 10
var med = preload(“res://medkit.tscn”)
var health

var player = null

func _ready():
set_process(false)
add_to_group(“zombies”)
health = max_health
emit_signal(“health_changed”, health * 75/max_health)

func _physics_process(delta):
if player == null:
$Node2D2/AnimatedSprite.play(“idle”)
return

var vec_to_player = player.global_position - global_position
vec_to_player = vec_to_player.normalized()
global_rotation = atan2(vec_to_player.y, vec_to_player.x)
move_and_collide(vec_to_player * MOVE_SPEED * delta)
$Node2D2/AnimatedSprite.play("walk")

func kill():
var m = med.instance()
get_parent().add_child(m)
m.position = position

queue_free()

func take_oof(damage):

health-=damage
emit_signal("health_changed", health * 75/max_health)
if health <= 0:
	kill()

func _on_visibility_body_entered(body):
if body.name == “Player”:
player=body

func _on_VisibilityNotifier2D_screen_exited():
player = null

Are you using a tilemap? if so you may want to look into the Navigation2D node.

Check out this video on tilemaps and nav2d and you should be ready to make it happen:

https://www.youtube.com/watch?v=0fPOt0Jw52s

TheThirdPerson | 2019-04-24 19:20

thank that will be useful!!!

thoma | 2019-04-24 22:45