I’m making an NPC have a laser attack, and I use raycasting in order to do this. The ray is set to hit a position of a part, and I’m getting the direction of the ray correctly. The problem is, the ray sometimes doesn’t hit anything, even though it should.
Here’s the code for the ray:
--Cast a ray
local origin = spirit.PrimaryPart.Position
print("origin: " .. tostring(origin))
local lookVector = CFrame.new(origin, pos).LookVector
print("look vector: " .. tostring(lookVector))
local rayParams = RaycastParams.new()
local team = teams:FindFirstChild(spirit:GetAttribute("Team") .. "s")
local characters = team:GetPlayers()
for index, player in pairs(characters) do
characters[index] = player.Character
end
rayParams.FilterDescendantsInstances = {table.unpack(characters), table.unpack(module.getSpirits(spirit:GetAttribute("Team")))}
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
local rayResult = workspace:Raycast(origin, lookVector, rayParams)
if not rayResult or not rayResult.Position then
print("ray didnt hit anything")
return
end
-
What do you want to achieve? I was to make a laser attack with raycasting.
-
What is the issue? The ray sometimes hits nothing, and I don’t know why.
-
What solutions have you tried so far? I searched for similar posts, but they all said that the ray just doesn’t hit anything, and my ray should guarantee a hit.
The reason it’s guaranteed to hit is because the NPC is in a map, and the map is completely enclosed. The origin of the ray is in the map, I’ve checked, and if it helps, sometimes the direction of the ray is slightly different each time it’s fired, and it always has the same direction when firing the laser.