Hello.
I am having trouble visualizing my raycast. Here is an example:
As you can see, the white visualization part is not perfectly aligned with the part projecting the ray.
Here is my code, any help is appreciated. Thank you.
local runService = game:GetService("RunService")
local folder = script.Parent
local reflection = folder.Reflection
local cue = folder.Cue
local aim = folder.Aim
local maxDistance = 500
runService.Heartbeat:Connect(function()
local cueCframe = cue.CFrame
local currentPosition = cueCframe.Position
local currentNormal = cueCframe.RightVector
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {cue, reflection, aim}
local raycast = workspace:Raycast(currentPosition - Vector3.new(0, 0, cue.Size.X / 2), currentNormal * maxDistance, params)
local aimSize = aim.Size
local intersection = (raycast and raycast.Position or currentPosition + (currentNormal * maxDistance))
currentPosition = (cue.CFrame * CFrame.new(cue.Size.X / 2, 0, 0)).Position
if (raycast) then
reflection.Transparency = 0
local normal = raycast.Normal
local reflectionNormal = (currentNormal - (2 * currentNormal:Dot(normal) * normal))
aim.Size = Vector3.new(aimSize.X, aimSize.Y, raycast.Distance)
aim.CFrame = CFrame.new(currentPosition, intersection) * CFrame.new(0, 0, -raycast.Distance / 2)
reflection.CFrame = CFrame.new(intersection, reflectionNormal * maxDistance) * CFrame.new(0, 0, -2.5)
else
reflection.Transparency = 1
aim.Size = Vector3.new(aimSize.X, aimSize.Y, maxDistance)
aim.CFrame = CFrame.new(currentPosition, intersection) * CFrame.new(0, 0, -maxDistance / 2)
end
end)
aim
is the white visualization part, cue
is the part projecting the ray, and reflection
is the part visualizing the normal.