Hello!
Introduction
Today I made a quick lil’ module for better raycasting, because quite frankly, I really needed it.
I understand other developers also have some raycasting problems that need solving, and I’m here to help!
Installation
First thing’s first – you will need the module.
You can get it here, or use the downloadable source.
Usage
See the following API:
[RayObject] Ray RaycastModule.new(RaycastParams Params, Double Penetration, Boolean OnlyCanCollide, Boolean AllowInvisible, Int MaxPenetrations)
Creates the ray object. All parameters are optional.
See the following changeable properties of the Ray instance:
ray.Parameters;
ray.Penetration;
ray.OnlyCanCollide;
ray.AllowInvisible;
ray.MaxPenetrations;
==========
[RaycastResult] Result, [Int] NumberOfPenetrations RayObject:Raycast(Origin, Direction)
Performs a raycast from a ray instance.
Returns the raycast result and number of penetration cases.
==========
Example Code:
local Player = game.Players.LocalPlayer
local Cam = workspace.CurrentCamera
local Context = game:GetService("ContextActionService")
local Text = script.Parent:WaitForChild("TextLabel")
local Raycast = require(script.Raycast)
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Player.Character or Player.CharacterAdded:Wait()}
local ray = Raycast.new(Params, 1, true, false, 3)
Context:BindAction("Click",function(_, State)
if State == Enum.UserInputState.Begin then
local cf = Cam.CFrame
local origin = cf.Position
local dir = cf.LookVector
local Result = ray:Raycast(origin, dir * 100)
if Result then
Text.Text = Result.Instance.Name
else
Text.Text = "no result!"
end
end
end, false, Enum.UserInputType.MouseButton1)
Hope this helps you with your raycasting!
I’m open to any comments, questions, or concerns