Raycast+ || Raycasting with penetration, property ignores, and more!

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 :slight_smile:

30 Likes

Looks good but there is a slight edge case where AllowsInvisible wouldn’t work.

BaseParts have a property called LocalTransparencyModifier, which developers can set and this is what CameraMode uses for its occlusion mode too I believe. So you may want to check that too. Just acts as a multiplier on the client for a BasePart’s transparency

Kind of an extreme/far off thing and easy to change , but it is possible so thought I’d throw a heads up

8 Likes

I appreciate the feedback!
I’ll be sure to work on that soon.

You should add spread to the raycast module.

This module reads the z size of an object that it hits, instead of the side the shot came from. An example would be if you shoot from above the part, down onto the part’s top, you would need to know the distance between the top of the part, and the bottom of the part. This module always returns from front to back of the part. How would I remedy this situation?

This module is defective in terms of penetrative detection, and I wouldn’t use it for newer uses. It’s quite old and therefore has old, bad code.