RAYVIZ | A Helpful Raycast Debugger Tool
A simple, yet powerful, raycast visualizer for all of your debugging needs!
INTRODUCTION
Why would I make this you may ask? Well; recently, I was working on a system that relied on raycasting, but while working with it, I was having troubles visualizing in my head on where the raycasts were actually shooting from and where it they were directed towards (I’m not the best at vector math yet), and while there are resources to visualize where the raycast will go, it started to make my code pretty unreadable and spaghetti like, causing me to spend more time to fix the aesthetic of my code so I don’t get confused from looking at my own code. Due to this problem I have faced, I made this module as an easy and fast solution for debugging raycast related problems!
HOW DOES IT WORK?
RayViz is an OOP structured raycast visualization module that helps with abstracting and streamlines the process of debugging raycast related issues by visualizing where the raycast has went.
RayViz is also 99% typescripted, so usages should become slightly more easier when using this module! The method :Destroy() is getting a typescript warning due to the function setting the current metatable to nil (idk how to fix it, sorry)
API
RaycastVisualizer: The main “handler” for visualizing raycasts.
RaycastVisualizerSettings: Before creating a new RaycastVisualizer
, we must first create settings that our RaycastVisualizer will use!
RaycastVisualizerSettings
SETTING NAME | DataType | Description |
---|---|---|
LOG_MESSAGES | boolean | Logs messages to the output. |
AUTO_CLEANUP | boolean | If true, it will auto clean all drawn raycasts from the current RaycastVisualizer. |
MAX_VISUALIZERS | number | If autoclean is enabled, this setting will cleanup after the specified amount of raycast visualizers were made. |
local Settings = {
LOG_MESSAGES = false,
AUTO_CLEANUP = true,
MAX_VISUALIZERS = 50,
}
Draw: This method actually creates the visualization. The method needs has a Origin
argument, which is needed to see where the raycast starts at. Next is the EndPosition
, which is where the end position that the raycast has made it too. Color is the color for the visualization and VisualsDuration is the time before the visualization is automatically cleaned-up.
(Origin: Vector3, EndPosition: Vector3, Color: Color3, VisualsDuration: number?)
GetVisualizedCount: This method returns the amount of visuals the current RaycastVisualizer has currently active.
GetVisualizedCount: This method returns ALL of the visuals ALL RaycastVisualizer’s have made.
Cleanup: This method cleans up all visuals the current RaycastVisualizer has made.
CleanAllVisuals: This method cleans up ALL visuals made by ALL RaycastVisualizer’s.
Destroy: This method cleanssup the current RaycastVisualizer visuals, and sets the current metatable to nil.
Please note the following:
-
Currently, the module counts in intervals of two when creating a visualization (for the pointer cone and the line). So having a maximum draw amount of 10 would yield 5 draw calls.
-
This module wasn’t tested a lot so if there is a bug please report it in the comments and describe how the bug was found/created.
-
There are a few minor visual bugs present that I do know of, until I have the time fix them, this message will remain here.
HOW TO USE
This module is extremely easy to setup and use, here is an example on how to use the module.
--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--// Modules \\--
local RaycastVisualizerClass = require(ReplicatedStorage.Shared.Modules.Classes.RaycastVisualizerClass)
--// Instances \\--
local Sphere = script.Parent
--// Class Objects \\--
local RaycastVisualizer = RaycastVisualizerClass.new({AUTO_CLEANUP = true, MAX_VISUALIZERS = 1500, LOG_MESSAGES = false})
--// Private Functions \\--
local function getRandomDirection()
return Vector3.new(math.random() - 0.5, math.random() - 0.5, math.random() - 0.5)
end
while true do
task.wait()
local raycast = workspace:Raycast(Sphere.Position, getRandomDirection() * 500)
if not raycast then continue end
RaycastVisualizer:Draw(Sphere.Position, raycast.Position, Color3.fromRGB(255, 38, 0), 1)
end
OFFICIAL RELEASE
I plan on adding support for visualizing raycasts that haven’t hit any surfaces, but for now, this only works for successful hits from the raycasts.