Raycast to detect a non-collidable character

I made a raycast gun and I want it to damage a players’ character that is non-collidable. The players’ character is in a collision group that doesn’t collide with anything.

When I try to target the character, it would just go through it. However, if I shot a part that is CanCollide to false, it would still detect it. So, I am pretty stumped here. Is there any way to work around this?

1 Like

You can’t shoot a Ray to detect the Humanoid specifically, as it doesn’t have a physical hit-box. You have to Raycast for parts( arms, legs, head ) inside the character, and get the Humanoid from the parent of those parts.

if RayHitPart.Parent:FindFirstChild("Humanoid") then

RayHitPart.Parent.Humanoid:TakeDamage()

end

Also, collision groups don’t have a affect on Raycasting. Collision groups only relate to the collisions between physical objects.

Can we see your code, so we can try to figure out what is going on?

2 Likes

I’ll clear up the initial post as I was referencing humanoid loosely to the player’s character. Sorry about that. Here’s the code for the raycast.

-- Creates a raycast to damage humanoid 
onZap.OnServerEvent:Connect(function(player,position)
	local origin = lens.Position
	local direction = (position - origin).Unit
	
	local raycastParams = RaycastParams.new()
	-- Blacklists the tool
	raycastParams.FilterDescendantsInstances = {tool,player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(origin,direction * MAX_DISTANCE,raycastParams)
	
	local intersection = raycastResult and raycastResult.Position or origin + direction
	local distance = (origin - intersection).Magnitude
	
	-- Runs if it detected base part
	if raycastResult then
		
		local hitPart = raycastResult.Instance
		local hitParent = hitPart.Parent
		
		if hitParent:FindFirstChildWhichIsA("Humanoid") then
			-- Code to damage player
		end
		
	end

	-- Creates beam effect
	local beam = createBeam(intersection)	
	
end)

Is the code just failing to detect characters, or does the Ray fail to register hitting normal parts too? Is it specifically characters that it can’t detect, or is it everything in general?

It just fails to detect characters that I’ve set to be not collidable through collision groups.

I’m also just testing this on a non-collidable character model that I have for play testing.

Does the character rig have a Humanoid?

I don’t see anything wrong with that code. The only thing I can think of that could be the issue, is the position parameter you are passing from the client. Is this value affected in any way by collision groups, or anything of the such?

Yeah the rig has a humanoid. The position parameter is the mouse.Hit.Position, so I don’t really know if is affected by anything else. It is how it detects what is clicked, and it works fine on other character rigs that are collidable.

Does the rig’s collisions group collide with the Default collision group?

It may have something to do with this:

1 Like

No, it doesn’t, and the thread you posted seems to answer my questions.

I’ve looked through the thread and it seems like I will have to do extra steps to get this to work. I don’t understand why Roblox would consider this as a feature though. You can still detect parts with raycast that are just set to CanCollide false. Why can’t we do the same with parts that can’t collide with the Default collision group?

Not the most straightforward solution, but it’s something. Thank you for helping me! I’ll keep it open if there are any recent updates towards this

1 Like