# Problems with enemy pathfinding

edited June 25

Hello,Guys! I'm trying to program enemy pathfinding, but I don't know how to take into account collision.
Because of it if player hide behind the middle of the wall, enemy will stuck in middle or corner of the wall.
Collision is CircleShape2D with Radius 32.
Here's the enemy code:

``````extends KinematicBody2D

var path : PoolVector2Array

var acc := Vector2(0,0)
var vel := Vector2(0,0)
const friction := 0.7

func _physics_process(delta) -> void:

path.remove(0)
#vel += acc
#vel -= Vector2(min(abs(vel.x), friction) * sign(vel.x),min(abs(vel.y), friction) * sign(vel.y))

vel = Vector2(1,1)
vel = move_along_path((abs(vel.x)+abs(vel.y))*delta*60)
print(vel)
vel = move_and_slide(vel*60) / 60
acc = Vector2(0,0)

func move_along_path(distance : float) -> Vector2:

var start_point := global_position
var result_velocity := Vector2(0,0)
for i in range(path.size()):
var distance_to_next := start_point.distance_to(path)
if path.size() == 1 and distance > distance_to_next:
return result_velocity
elif distance <= distance_to_next:
result_velocity += start_point.linear_interpolate(path,distance / distance_to_next) - global_position
return result_velocity

result_velocity += start_point.linear_interpolate(path,distance / distance_to_next) - global_position
distance -= distance_to_next
start_point = path
path.remove(0)
return result_velocity

#Used if bullet collides with enemy
func hit(body: Node2D):

acc = body.vel
body.queue_free()
\$oof.play()
``````

Tags :

the issue here is that the pathfinding does not account for a size (radius 32 in your case).

In case you are using a tilemap you can try one of the following

• make your tiles (sprite and collision) smaller than the gridsize of your tilemap. This should give your enemies some room to go around corners
• set the third argument of `get_simple_path` to `false`.

With these two things i managed to make my enemies go around corners properly.

Hope i could help

the issue here is that the pathfinding does not account for a size (radius 32 in your case).

In case you are using a tilemap you can try one of the following

• make your tiles (sprite and collision) smaller than the gridsize of your tilemap. This should give your enemies some room to go around corners
• set the third argument of `get_simple_path` to `false`.

With these two things i managed to make my enemies go around corners properly.

Hope i could help