Weird issues with Raycasts on perfect axes

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.

1 Like

hold please I’ll upload the video to YouTube

1 Like

I think its because of how you fire the raycast and position it.

Make a print of what part it hits when obstruction is hit.

1 Like

Futher down the script, I do. it says the obstruction is nil. I don’t understand how.
I did some experimenting and found that it was hitting a house in the opposite direction halfway across the map. I’m gonna screw around with a math for a while and see if I can get it to work.

1 Like

I got it! I’m so stupid, I made the raycast ignore the jaw creature! Ha, ha, ha. Sorry to waste your time.

1 Like

ermmmmmmmmmmmmmmmm what the sigma

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.