EzRays Module - Save tons of time with raycast code! [v1.2]

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.

Thanks for reading and I hope this helps!

15 Likes

I find this module redundant, as it just does what WorldRoot:Raycast does with way more complication. This won’t reduce time nor will it make the code more efficient.

5 Likes

it’s to simplify the creation of raycastparams and getting the returns, I am also personally very used to the old raycast code and this resembles it a lot. I never said this is some grand magic module but its still better to type in a single line than to type in like 4 for raycastparams + 4 to get the returns

4 Likes

I don’t find what’s the difference between your module and using the traditional way to raycast, for now everybody can make a function that processes a ray and returns everything, I don’t really find your module too necessary to be used, if your module maybe had some other features, maybe it had a chance to be used by many people.

However don’t take this too personally, but if you wanna contribute to the community, you have to come with something that is indeed useful. :slight_smile:

1 Like

Thank you, I will probably use this / try it out.

2 Likes

I made it cus Ray.new is being deprecated. I know that it doesn’t mean Ray.new won’t work anymore but I just prefer to always stay on the safe side, and in this case use RaycastParams.

1 Like

Updated the module with new important functions.

RaycastParams should only be created once for reoccuring raycasts (for example a bullet projectile) or for rays that share the same parameters. If you need to edit a parameter then change the RaycastParams’s params (clap)

I made sure it’s simple to do with the new module update, check out the new functions in the beginning (and the end for more detail) of the post!

1 Like