problem with navigation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Viltra

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!

RAYCAST2D CONTROLLING FUNCTION:

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 : 2022-11-13 04-34-51

Full player code in pastebin : extends KinematicBody2Dconst PlayerHurtSound = preload("res://PlayerHurtSoun - Pastebin.com

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

:bust_in_silhouette: Reply From: Jed Stevens

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.