Hello Godot community,
I’m currently working on a project where I need to perform raycasting in Godot Engine. I’ve encountered an issue where the raycast works perfectly when I draw the collision polygon points in the viewport, but it doesn’t work when I add them using code.
Here’s a simplified version of my code:
var collision_points := PackedVector2Array()
@onready var collisionPolygon2d = $StaticBody2D/CollisionPolygon2D
@onready var raycast2d = $RayCast2D
func _ready():
collision_points.append(Vector2(-832, -144))
collision_points.append(Vector2(1552, 88))
collision_points.append(Vector2(1120,416))
collision_points.append(Vector2(-200, 272))
func _process(delta):
collisionPolygon2d.polygon[0] = collision_points[0]
collisionPolygon2d.polygon[1] = collision_points[1]
collisionPolygon2d.polygon[2] = collision_points[2]
collisionPolygon2d.polygon[3] = collision_points[3]
print(raycast2d.is_colliding())
manual add vertex
add vertex using code

