0 votes

i am using AI to control my player nodes, along with a navigation node and some obstacle avoidance code, but sometimes the raycast2D nodes get stuck on some collisionshapes for no apparent reasons, code below along with all relevant info.

Thank you in advance for your help!


func avoid_obstacles() -> Vector2:
var avoid_vel = Vector2.ZERO
raycasts.rotation = navigate().angle()

for raycast in raycasts.get_children():
    if raycast.is_colliding():
        var obstacle_dir = (global_position - raycast.get_collision_point()).normalized()
        var obstacle_interest = navigate().normalized().dot((raycast.get_collision_point() - global_position).normalized())
        if obstacle_interest >= 0.5 && raycast.name == "RayCast2D":
            avoid_vel += obstacle_dir.rotated(deg2rad(90))
        elif obstacle_interest >= 0.5 && raycast.name == "RayCast2D2":
            avoid_vel += -velocity
        elif obstacle_interest >= 0.5 && raycast.name == "RayCast2D3":
            avoid_vel += obstacle_dir.rotated(deg2rad(-90))
        elif obstacle_interest >= 0:
            avoid_vel += obstacle_dir
return avoid_vel.normalized() * avoid_force

i believe this is due to the raycast2Ds not colliding correctly but i have no idea how to fix it, here is a video of how that looks : https://streamable.com/1asnho

Full player code in pastebin : https://pastebin.com/W8Gjtsq8

PS : i am not using a tilemap but a navigationpolygoninstance.

Godot version v3.3.4.stable.official
in Engine by (23 points)

1 Answer

0 votes

I have a feeling that you are using the ray cast on the same collision layer as the terrain; although it may be useful for you to detect things; there are ways for ray casting that will not impart the collision response on your character. I would consider an alternate implementation of your ray casts if they are not being used as an object that will spatially respond to collisions (ie impart a force on your character)

I only say this because they don't seem to be a character collider and look more like detection rays. Also the box that you clip on is the only ray that gets hit 'head on' which I think may be how the glitch becomes reproduce-able.

Without seeing your node tree it would be hard for me to guess much more.

by (136 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.