Raycast not properly detecting parts

Greetings,

I am in a bit of trouble with the following script:

function raycast()
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {NPCsFolder, Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true

	local raycastResult = workspace:Raycast(Character.HumanoidRootPart.Position, NPC.Head.Position, raycastParams);
	if raycastResult then
		workspace.Baseplate.Color = Color3.new(0,1,0); --green
	else 
	workspace.Baseplate.Color = Color3.new(1,0,0); --red
	end
end

Now, the issue is as follows:
Screenshot (422)
Proper hit
Screenshot (423)
Improper hit

Now, as you can see, the raycast does not detect the part when the player is standing on right side of it. Does anyone know why this is the case?

The direction argument is a directional vector, not positional. You can find whichever directional vector you need by doing goal - origin. If you’re casting from the root, in the direction of the NPC’s head, you should use npcHead - root.

2 Likes