Hello everyone!
After hearing the recent news about there being a new raycasting method I decided to create a module which would simplify it since it’s a bit more complex than the classic one.
UPDATE:
In the first version I have missed a very crucial thing - the fact that you don't need to create new RaycastParams for each raycast...facepalm...So I added the following functions: ezrays.newParams() and ezrays:Raycast() (and ezrays:Reflect(), credit goes to @Crazyman32 :V)
The old ezray.new() function stays in place and functions the same of course, however now it returns the raycastparms it creates in the 5th return!
What's the use?
The module removes the hassle of creating RaycastParams and getting all the return variables.
The usage is quite simple and resembles the classic way much more (asides from the new arguments of course), and is just simpler than the new method in general:
local HitPart,HitPosition,HitNormal,HitMaterial,RayParams = EzRay.new(Origin,Direction,IgnoreList,FilterType,IgnoreWater,CollisionGroup)
Compared to:
local rayParams = RayParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist / Whitelist
rayParams.FilterDescendantsInstances = (your_table)
rayParams.IgnoreWater = true/false
rayParams.CollisionGroup = (your_collision_group_here) or "Default"
local rayResult = workspace:Raycast(Origin,Direction,rayParams)
if rayResult then
local HitPart = rayResult.Instance
local HitPosition = rayResult.Position
local HitNormal = rayResult.Normal
local HitMaterial = rayResult.Material
end
And you end up saving so many lines and a lot of time on rays.
You can get it here:
https://www.roblox.com/library/5485904658/EzRays
USAGE:
Sample code:
local Origin = SomePart.Position
local MaxDistance = 9001
local Direction = SomePart.CFrame.LookVector * MaxDistance
local IgnoreList = {SomeOtherPart, AnotherSomePart}
local EzRay = require(path_to_module)
local hitpart, hitpos, hitnorm, hitmat = EzRay.new(Origin, Direction, IgnoreList, 'Blacklist', true)
if hitpart then
-- run your code here!
end
You can pass “Blacklist” or “Whitelist” strings as arguments for FilterType, or Enum.RaycastFilterType.Blacklist/Whitelist
Essentially you only need to pass Origin and Direction, the rest have defaults if nil.
You can also change the DefaultFilterType in the module if you wish. If it’s set to “Blacklist” or “Whitelist” then if the FilterType argument in ezray.new is nil then the rayparams.FilterType will be set to the desired filter type.
local HitPart,HitPos,HitNormal,HitMaterial,RayParams = EzRays.new(Origin,Direction,IgnoreList,FilterType,IgnoreWater,CollisionGroup)
-- Creates new raycastparams and does workspace:Raycast with the given rayparams and arguments, returns all the RayResult variables
local RayParams = EzRays.newParams(IgnoreList,FilterType,IgnoreWater,CollisionGroup)
-- Use this if you don't want to bother writing multiple lines for new RaycastParams, returns the created RaycastParams.
local HitPart,HitPos,HitNormal,HitMaterial = EzRays:Raycast(Origin,Direction,RayParams)
-- Use this if you don't want to bother writing multiple lines to get the returns of the RayResult, returns all the RayResult variables.
local ReflectedNormal = EzRays:Reflect(StartNormal,ResultNormal)
-- StartNormal should be the Direction of the ray you're trying to reflect, ResultNormal is the ray result Noraml (HitNormal), Returns a Vector3 of the reflected normal. Thanks Crazyman32!
A tiny bit more documentation is available inside of the ModuleScript.