Hi guys! New to Godot.
I have a CharacterBody2D that moves as usual with WASD and faces the mouse. When I press spacebar, it throws out a boomerang that goes in a preset direction (using a throw_start and throw_end node to set the direction) that's always the same.
I am trying (and currently failing) to make a system to raycast out in the same direction as the ball, drawing out a line as preset distance (reusing the above throw_start and throw_end nodes). if it collides with something, then I want it to instead stop drawing the first line where the collision would happen (which should be where the boomerang would hit the wall too) and then it will calculate the bounce trajectory (which, again, should be the same as the boomerangs) and then draw a second line out so that you can preview the bounce.
Right now the first part works, I can draw the raycast out the length between the throw_start and the throw_end. However it changing the length when colliding doesn't work (it's no longer the same exact straight line) and the bounce is completely different. Any help is appreciated.
Any help is appreciated.
The Raycasting code
`
extends Node2D
class_name Laser_Cast
@onready var first_raycast = $"First Raycast"
@onready var first_line_2d = $"First Line2D"
@onready var second_raycast = $"Second Raycast"
@onready var second_line_2d = $"Second Line2D"
@onready var throw_start = $"../Throw Start"
@onready var throw_end = $"../Throw End"
@export var first_raycast_length = 100.0
func _ready():
first_raycast.position = throw_start.position
first_raycast.target_position = throw_end.position
func _process(_delta):
handle_first_laser()
handle_second_laser()
func handle_first_laser():
var is_colliding = first_raycast.is_colliding()
if (is_colliding):
first_line_2d.points = [first_raycast.position, to_local(first_raycast.get_collision_point())]
else:
first_line_2d.points = [first_raycast.position, first_raycast.target_position]
func handle_second_laser():
var is_colliding = first_raycast.is_colliding()
if not is_colliding:
second_line_2d.points = []
return
var collision_point = first_raycast.get_collision_point()
var collision_normal = first_raycast.get_collision_normal()
var bounce_direction = collision_normal.bounce(collision_normal)
var line_start = to_local(collision_point)
var line_end = to_local(collision_point + bounce_direction * 200)
second_line_2d.points = [line_start, line_end]
`
