Polygon Raycast2D

Godot Version

4.2.1

Question

I have been trying to implement the polygon2D raycast as shown in the video.

I would like to use it as a vision system for my Enemies as it collides with walls so the Enemies wont be able to see through walls.

I think it was made for godot 3.5 and i have tried to adapt the code to 4.2.1. But i still can’t get it to work.

The problem is that whenever i try to set the polygon_2d points it doesn’t work. (It won’t draw it ingame).


@onready var polygon_2d: Polygon2D = $AggroRange/Polygon2D
var FOV_increment = 2 * PI / 60


func _ready():
	draw_target_area()

func draw_target_area():
	set_target_area(get_FOV_circle(global_position, 300))



func set_target_area(points: PackedVector2Array):
	polygon_2d.set_polygon(points)
	


func clear_target_area():
	set_target_area(PackedVector2Array())


func get_FOV_circle(from: Vector2, radius):
	var angle = FOV_increment
	var points = PackedVector2Array()
	while angle < 2 * PI:
		var offset = Vector2(radius, 0).rotated(angle)
		var to = from + offset
		points.append(to)
		angle += FOV_increment
	return points

Okay so it seems to be drawing. It’s just that it doesnt draw it at the global_position for some reason. No clue why or how to fix.

EDIT: After alot of tinkering i found the problem. I had to user owner.global_position for some reason even though it’s in a class that the main script extends.

However now i would like some help with space_state.
Like in the video at 8:40. The space state only takes one argument in 4.2.1 it seems. But she enters 4 arguments.

I found out how to set the arguments as you can see in the get_FOV_circle function.

However now the problem is that it doesnt seem to detect collision layer 2. Because when the Polygon2D collides with a wall on Collision Layer 2 it doesn’t print “Detected” in the if statement and it still draws over the wall. The end result should be that it doesnt draw over walls that are on collision layer 2.

class_name Enemy extends Creature

@onready var polygon_2d: Polygon2D = $AggroRange/Polygon2D
@onready var space_state = get_world_2d().direct_space_state


var FOV_increment = 2 * PI / 60


func _physics_process(delta):
    draw_target_area()

func draw_target_area():
    set_target_area(get_FOV_circle(owner.global_position, 300))



func set_target_area(points: PackedVector2Array):
    polygon_2d.set_polygon(points)
    


func clear_target_area():
    set_target_area(PackedVector2Array())


func get_FOV_circle(from: Vector2, radius):
    var angle = FOV_increment
    var points = PackedVector2Array()
    while angle < 2 * PI:
        var offset = Vector2(radius, 0).rotated(angle)
        var to = from + offset
        var params = PhysicsRayQueryParameters2D.new()
        params.from = from
        params.to = to
        params.exclude = [] 
        params.collision_mask = 2 
        var result = space_state.intersect_ray(params)
        if result:
            print("Detected")
            points.append(result.position)
        else:
            points.append(to)
        angle += FOV_increment
    return points