Ah I see. You basically just do one raycast after another, and check if the current one is closer than the last one.
Here, I packaged it into a function for you. I left comments that explain what it does. If you have any questions just ask!
-- raycasts from one origin in multiple directions
-- parameters:
--
-- origin: Vector3 the origin of all the rays
-- directions: Array<Vector3> a list of all the directions
-- rayParams: RaycastParams the parameters for each raycast (optional)
local function MultiRaycast(origin, directions, rayParams)
local finalResult = nil
local finalDistanceToOrigin = nil
-- for each of the directions passed in,
for _, dir in directions do
-- do a new raycast
local result = workspace:Raycast(origin, dir, rayParams)
if result then
if finalResult then
-- check the distance of this raycast's hit
local distanceToOrigin = (result.Position - origin).Magnitude
if (distanceToOrigin < finalDistanceToOrigin) then
-- and if its the closest one yet, replace finalResult
-- with this one
finalResult = result
finalDistanceToOrigin = distanceToOrigin
end
else
-- this is the first raycast that hit something
finalResult = result
end
end
end
return finalResult
end
You just it instead of Workspace:Raycast with a table of directions like this:
local origin = HRP.Position
local directions = {
HRPCFrame.LookVector * REACH,
Vector3.new(0, REACH, 0),
Vector3.new(0, -REACH, 0)
-- ...any other directions you want to check
}
local result = MultiRaycast(origin, directions)
if result then
-- do whatever
end