Attention | Topic was automatically imported from the old Question2Answer platform. | |
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.