I have no idea why this isn’t detecting in front of the part. I am trying to make a script where a part shoots out another part then that part will reflect depending on if the ray hits something and the ray reflects. The intersection position for the first ray result is good:

As you can see it perfectly intersects and reflects on the surface.
The second intersection though, does not reflect on the surface the ray was hit on. Instead, it reflects inside the part.

Or rather it intersects behind it. Isn’t it supposed to be in front of the part its hitting. Also ignore the ray that is going in a straight line it doesn’t have to do anything with it. I also changed up the reflect formula a little bit. One thing I realized when learning these reflects is that you can create different formulas and get different results.
Script
local start = game.Workspace.Start
local function createPart()
--Creating part
local off = CFrame.new(0,0,-1.5)
local part = Instance.new("Part",workspace)
part.Name = "Pea"
part.Size = Vector3.new(1,1,2)
--part.Transparency = 1
part.BrickColor = BrickColor.Green()
part.Anchored = true
part.CFrame = start.CFrame:ToWorldSpace(off)
return part
end
local function createNewParams(table)
--Raycast Params
local params = RaycastParams.new()
params.FilterDescendantsInstances = table
params.FilterType = Enum.RaycastFilterType.Blacklist
return params
end
local function reflect(v,n)
local reflection = v - 2 * v:Dot(n) * n
return reflection
end
local function drawray(origin,direction,tableList)
local mid = origin+direction/2
local part = Instance.new("Part",workspace)
--part.Transparency = 1
part.Anchored = true
part.BrickColor = BrickColor.Red()
part.Material = Enum.Material.Neon
part.Size = Vector3.new(0.1,0.1,direction.Magnitude)
part.CFrame = CFrame.new(mid,origin)
table.insert(tableList,part)
return part
end
local function bounce(part,raypart)
part.Orientation = raypart.Orientation
part.CFrame = part.CFrame - raypart.CFrame.LookVector
end
local function castRay(part,table)
--Casts ray for part only
local origin = part.Position
local direction = part.CFrame.LookVector * 1.25
--Creating parameters,drawing and casting ray
local params = createNewParams(table)
local result = workspace:Raycast(origin,direction,params)
--local rayPart = drawray(origin,direction,table)
return result
end
function checkRay(part,table)
local result = castRay(part,table)
if result then
print(result.Instance)
local n = result.Normal
local x = result.Position
local reflection = reflect(x,n)
local newVector = x + reflection
--Creating parameters
local newParams = createNewParams(table)
local newRayPart = drawray(x,newVector,table)
if result then
for i = 1,10,0.01 do
bounce(part,newRayPart)
checkRay(part,table)
wait()
end
elseif not result then
print("No wall has hit this ray")
return false
end
end
end
local function shoot(part,table)
for i = 1,50,1 do
part.CFrame = part.CFrame + Vector3.new(0,0,1)
checkRay(part,table)
wait()
end
end
local function main()
local part = createPart()
if part then
local listToBeIgnored = {part,start}
shoot(part,listToBeIgnored)
end
end
main()
If you want to test this out, all you need it a part called start put it in the workspace and have a couple of walls by it so it can reflect it. This is probably the only I have with this script, idk if this is something I can control or if it is just roblox ray detection system acting weird.