Problems with enemy pathfinding

RandomNameRandomName Posts: 1Member
edited June 25 in Programming

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()

Tags :

Best Answer

  • vinpogovinpogo Posts: 26
    Accepted Answer

    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

Answers

  • vinpogovinpogo Posts: 26Member
    Accepted Answer

    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

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file