- Edited
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
onready var navigation := get_node("../Navigation2D")
onready var player := get_node("../Player")
var path : PoolVector2Array
var acc := Vector2(0,0)
var vel := Vector2(0,0)
const friction := 0.7
func _physics_process(delta) -> void:
path = navigation.get_simple_path(position,player.global_position)
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[0])
if path.size() == 1 and distance > distance_to_next:
return result_velocity
elif distance <= distance_to_next:
result_velocity += start_point.linear_interpolate(path[0],distance / distance_to_next) - global_position
return result_velocity
result_velocity += start_point.linear_interpolate(path[0],distance / distance_to_next) - global_position
distance -= distance_to_next
start_point = path[0]
path.remove(0)
return result_velocity
#Used if bullet collides with enemy
func hit(body: Node2D):
acc = body.vel
body.queue_free()
$oof.play()