KinematicBody2D Getting Stuck

MetalCavemanMetalCaveman Posts: 11Member

Hello, I am having some issues with a KinematicBody2D, it is supposed to represent an enemy that chases the player around, however, when it runs into a wall or other objects, it gets stuck and takes a long time to free itself and resume chase.

I have tried changing the collision shape from a capsule to a circle, changing the radius, lowering the friction value (even setting it to 0) and the issue persists. I am using move_and_slide() to handle movement and I thought that would prevent it from getting stuck like that, but it hasn't worked out well.

Here's a screenshot of what I'm talking about:

https://i.imgur.com/ZXqmA0H.jpg

I'm thinking of using raycasts to either avoid the walls or give the enemy a shove in the opposite direction whenever it gets stuck, but I don't know how to handle it smoothly so movement is preserved and it doesn't look like it suddenly bounces or changes direction.

Here's the code that handles movement:

var velocity = Vector2()
var direction = Vector2()

func process_movement():
    direction = direction.normalized()
    velocity = direction

    if(is_sprinting):
        velocity = velocity * sprint_speed
    else:
        velocity = velocity * walk_speed

    velocity = move_and_slide(velocity)

Direction is obtained via a Navigation2D node and the get_simple_path() function.


Tags :

Comments

  • MetalCavemanMetalCaveman Posts: 11Member

    I've discovered part of the issue.

    Checking where the navigation mesh was I was able to tell that it was not properly set up, so part of the path was being calculated through the walls, instead of around them, I've since fixed that issue.

    Even so, there are still times when the enemy kinematicbody collides with an object and gets stuck there until the player moves in the opposite direction, which frees it allowing it to resume its path. I've set raycasts to detect collisions and try to shove it away from where it's colliding but this only works sometimes, not always.

    I guess I could just make all maps bigger so that the navigation mesh/polygon can be set in a way that avoids all obstacles and walls.

  • MegalomaniakMegalomaniak Posts: 1,585Admin
    edited August 24

    Is there empty margin left around your navigation mesh(es) so that there is no overlap what so ever?

    edit: also does the margin account for your units(player, enemies, etc.) size?

  • MetalCavemanMetalCaveman Posts: 11Member

    After checking and rechecking the nav mesh and the player and enemy scenes, seems I made quite the mistake, yeah, there's some overlap with the meshes and the size of both player and enemies is way too big in relation to the space available in most of the level scenes I have.

    Got to re-plan and re-make all of them now. :3

    Thanks for the help!

Leave a Comment

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