Raycast bouncing with Area2D node

Godot Version

4.6.3

Question

Hello! Beginner here. I’ve been trying for several hours now to implement what should be a very simple mechanic. I have a light beam that I want to bounce off the walls. I am struggling a bit to adapt code I’ve seen online because I am utilizing an Area2D node rather than a CharacterBody2D. This is because I needed to implement body_entered and body_exited signals that turn the light beam on/off.

Here is the hodgepodge of code I have currently. I know I’m very close and I think I’ve just been looking at the code so long I’m getting confused and in my head.

var player
var bounces := 1
var max_cast_to
var rot := 0.0
var beams := []

var velocity = Vector2(-200,200).normalized() * speed

const MAX_LENGTH := 2000

func _ready() -> void:
    beams.append(ray_cast)
	if ray_cast.enabled == true:
		for i in range(bounces):
			var new_raycast = ray_cast.duplicate()
			new_raycast.enabled = false
			new_raycast.add_exception(player)
			add_child(new_raycast)
			beams.append(new_raycast)
	
	max_cast_to = Vector2(MAX_LENGTH, 0).rotated(rot)
	ray_cast.target_position = max_cast_to

func _physics_process(float) -> void:
	if ray_cast.is_colliding():
		var points_array := [Vector2.ZERO]
		
		var raycast_collision = ray_cast.get_collision_point()
		
		var collision_normal: Vector2 = ray_cast.get_collision_normal()
		var incoming_vector: Vector2 = ray_cast.target_position.normalized()
		var reflected_ray = ray_cast.target_position.bounce(collision_normal).normalized()
		
		
		var next_point = raycast_collision + bounce_vector
		points_array.append(next_point)
		points_array.append(Vector2.RIGHT * MAX_LENGTH)

Here is a video clip of the prototype as well if that’s helpful.

RayCast2D doesn’t collide with areas by default. You need to enable RayCast2D.collide_with_areas

Thank you! But I think I worded this a bit confusingly. What I mean is that my raycast is the child of an Area2D node and not a CharacterBody2D. This means that I can’t use move_and_collide() like I see many tutorials utilizing so I’m having trouble figuring out how to make it work.

From my own troubleshooting I can confirm that the raycast IS in fact colliding with the walls, but I’m having trouble getting the raycast (and consequently the Line2D) the bounce off of it.