so if you don’t know what you’re doing with your raycasts, same! most people visualize their raycasts using parts, but (i think) that’s bad performance-wise; so I used beams!
local function initializeDebugRaycastBeams(amount: number, default_ray_length: number, holder: Instance)
local debug_beams = {}
local beam_index = 1
for _ = 1, amount do
local attachment = Instance.new('Attachment')
local target_att = Instance.new('Attachment')
local beam = Instance.new('Beam')
beam.Attachment0 = attachment
beam.Attachment1 = target_att
beam.Parent = holder
attachment.Parent = beam
target_att.Parent = beam
table.insert(debug_beams, target_att)
end
return function(origin: Vector3, direction: Vector3, result: RaycastResult)
local att = debug_beams[beam_index] -- get the current beam
att.WorldPosition = (result and result.Position or (origin + direction.Unit * default_ray_length))
beam_index = (beam_index % amount) + 1 -- cycle through
end
end
usage:
local ray_length = 10
-- create 1 beam with default ray length of 10
local visualize = initializeDebugRaycastBeams(1, ray_length, workspace.Baseplate)
local origin = workspace.Baseplate.Position -- where the ray is fired
local direction = Vector3.new(0, 1, 0) * ray_length -- up
local result = workspace:Raycast(origin, direction) -- do the raycast
visualize(origin, direction, result) -- will show a beam that shoots up 10 studs if theres nothing obstructing it
the visualize function can be used multiple times and will cycle through the initialized beams
me using it to visualize my DQN car ai obstacle detection rays:
one must imagine car trying to reach its target.
(the additional lag is from obs itself)