Cast ray just uses world root raycast, it just has an added visualizer.
local RayCaster = {}
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- Ensure that OriginPosition is a vector 3, rayDirection is a unit Vector and rayLength is an integer to multiply the direction by
function RayCaster:CastRay(originPosition, rayDirection, rayLength, toFilter, visualizeRay)
table.insert(toFilter, workspace.visualizedRays)
raycastParams.FilterDescendantsInstances = toFilter
local rayResult = workspace:Raycast(originPosition, rayDirection * rayLength, raycastParams)
local rayIntersectionPos = rayResult and rayResult.Position or (originPosition + rayDirection * rayLength)
if visualizeRay then
local dist = (originPosition - rayIntersectionPos).magnitude
local part = Instance.new("Part")
part.Parent = workspace.visualizedRays
part.CanCollide = false
part.Size = Vector3.new(0.02, 0.02, dist)
part.Material = Enum.Material.Neon
part.Anchored = true
part.BrickColor = rayResult == nil and BrickColor.new("Bright red") or BrickColor.new("Bright green")
part.CFrame = CFrame.lookAt(originPosition, originPosition + rayDirection) * CFrame.new(0, 0, -dist / 2)
game:GetService("Debris"):AddItem(part, 0.01)
end
return rayResult
end
return RayCaster
If I’m thinking about what you’re doing correctly then I’m assuming you want to cast the ray near the player’s right arm (like a weapon or tool). You can utilize the -UpVector (Downvector) and RightVector: