I’m having some strange bug with raycasting. I’ve got a turret and a bunch of jaw creatures. The turret is supposed to shoot and kill the jaw creatures by raycasting to the jaw creature’s torso. Everything works fine unless the jaw creature’s Torso is directly on the x- or z-axis of the turret. The turret prints an obstruction but refuses to tell me what’s there.
Here’s the raycasting algorithm.
if result and #result > 0 then
local leastDist = radius
local least = nil
for i, v in pairs(result) do
if v.Name == "Torso" and v.Parent.Zombie.Health > 0 then
if (script.Parent.Position - v.Position).Magnitude < leastDist then
print(v.Parent)
raycastparams.FilterDescendantsInstances = {script.Parent.Parent, v.Parent, workspace.Bullets}
local raycastresult = workspace:Raycast(script.Parent.Position, (script.Parent.Position - v.Position).Unit * 999, raycastparams)
print(raycastresult)
if raycastresult then
leastDist = (script.Parent.Position - v.Position).Magnitude
least = v.Parent
else
print("Obstruction")
end
end
end
end
print(least)
I have a video demonstration. The raycast prints “obstruction” for seemingly no reason. Does anyone know what’s wrong with it?
As you can see, jaw creatures 1 and 2 are killed just fine, but 3 is not because it is aligned with the turret. I have no clue why this would ever happen, and it works on all 4 axes.