So, i am triyng to make a raycaster with roblox’s new Workspace:Raycast()
method instead of the deprecated ray.new()
method, but there is an issue.
There is this weird issue or bug. I am not sure why, but it appears roblox’s raycast system is a bit broken when using CFrame.
You can clearly see the ray is not casting straight.
I doubt there is anything wrong with my code, but you can take a look anyways.
raycast bug.rbxl (27.2 KB)
local CameraPart = workspace:WaitForChild("CameraPart")
local LevelWorkspace = workspace:WaitForChild("LevelWorkspace")
local DebugLaser = Instance.new("Part") -- Useful for visualizing the rays
DebugLaser.Anchored = true
DebugLaser.Name = "DebugLaser"
DebugLaser.Parent = workspace
while true do
wait()
local ViewRayOrigin = CameraPart.CFrame.Position
local RenderDistance = 100
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Whitelist
Params.FilterDescendantsInstances = LevelWorkspace:GetChildren()
local ViewRayDirection = CameraPart.CFrame * CFrame.new(0, 0, -RenderDistance).Position
local ViewRay = workspace:Raycast(ViewRayOrigin, ViewRayDirection, Params)
if ViewRay and ViewRay.Instance and ViewRay.Instance:IsA("BasePart") then
local HitPart = ViewRay.Instance
local Distance = (ViewRayOrigin - ViewRay.Position).Magnitude
DebugLaser.Size = Vector3.new(0.2, 0.2, Distance)
DebugLaser.CFrame = CFrame.new(ViewRayOrigin, ViewRay.Position) * CFrame.new(0, 0, -Distance/2)
end
end