My understanding of Raycasting probably isn't good…

Hello.

Alright, so I have a problem with a raycast that I am trying to make. What I am trying to do is make a raycast shoot out of an npc, and I want the raycast to detect the rootpart of the player when its hit.

Here is what I have so far.;

while task.wait() do
	if point == nil then
		break
	end
	
	local raycast_params = RaycastParams.new()
	raycast_params.FilterType = Enum.RaycastFilterType.Include
	raycast_params.FilterDescendantsInstances = {enemy_npc}
	
	local right_raycast = workspace:Raycast(enemy_npc_hrp.Position, enemy_npc_hrp.CFrame.LookVector * 20, raycast_params)
	if right_raycast and right_raycast.Instance then
		if (right_raycast.Instance == hrp) then
			print("success")
		else
			print("fail")
		end
	end
end

All this code does is just always return the “fail” statement, and it never succeeds to detecting the player’s rootpart. Am I doing something wrong? Please let me know.

Thank you.

You want Exclude here, not Include, to make the npc itself invisible to the raycast.

Sorry for the wait, I was dealing with school and stuff, so I apologize.

Also, @MichaelKun101, enemy_npc is referring to the enemy inside of a folder, which is pretty much just a dummy named “Enemy”.

Anyways, I have tried both of your guys’ ideas, and it just doesn’t work. This time, instead of using the enemy’s hrp, I decided to code a part to follow the enemy’s hrp along the x axis, and I have also tried using raycasting on it too. I even tried using the ideas given here, and unfortunately nothing surprising happened.


Edit: this a 2.5d type of game by the way. Also, when the player joins the game, “success” only prints 1 time., the new code is down below;

local enemy_npc, enemy_npc_hrp, point
for i, character in ipairs(enemy_folder:GetChildren()) do
	enemy_npc, enemy_npc_hrp, point = character, character:WaitForChild("HumanoidRootPart"), character:WaitForChild("Point")
end

while task.wait() do
	
	local raycast_params = RaycastParams.new()
	raycast_params.FilterType = Enum.RaycastFilterType.Include
	raycast_params.FilterDescendantsInstances = {chr}
	
	local right_raycast = workspace:Raycast(point.Position, point.CFrame.RightVector * 30, raycast_params)
	if right_raycast ~= nil then
		if right_raycast.Instance == hrp then
			print("success")
		else
			print("fail")
		end
	end
end

… bruh.

I just found the solution.
Since this is a 2.5d fighting game, you know how the player will have their fists up in a fight, right? Well… It looks like the arms are blocking the detection of the HumanoidRootPart. Oh my god I am dumb for this.

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