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.
