#Polygon Raycast2D. Godot 4.2.1.

2 messages · Page 1 of 1 (latest)

vital swallow
#

https://www.youtube.com/watch?v=2dzBKXzGG5s
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).

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```

https://github.com/NitroxNova/Godot_Demos/tree/main/Targetable_Area_2D

00:00 intro
00:27 initial setup
02:42 set polygon2D
05:54 circle perimeter
08:39 raycast polygon
09:58 area2D

10:39 single target damage
13:20 red for out of bounds
15:23 add secondary target
18:15 draw secondary target

20:06 AOE damage
21:51 raycast arc
22:54 FOV cone
24...

▶ Play video
vital swallow
#

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.