It’s kinda lame that Roblox doesn’t include a built-in way for raycasts to ignore BaseParts with CanCollide set to false. This module fixes that. While I was at it, I modified the syntax to be easier on the eyes.
Using the module:
Raycast = require(game.ReplicatedStorage.RaycastsWithCanCollideChecking)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {}
raycastParams.IgnoreWater = true
RaycastResult = Raycast({
Params = raycastParams,
Origin = Vector3.new(0,0,0),
ToPoint = Vector3.new(0,5,0), -- Direction = Vector3.new(0,5,0),
CheckCanCollide = false -- true by default
})
I’ve also added a convenient ToPoint feature. If you’re raycasting to find a part between two points, and you don’t want to bother thinking about it, you can simply include a “ToPoint” parameter. If you want to use a LookVector instead, you can use the “Direction” parameter. If you use both, ToPoint overrides Direction.
Direction:
ToPoint:
The main mechanics are less than 50ish lines of code, but thinking about the recursion is just another layer of complexity that you might want to avoid if you’re making something already complicated.