Make player face whichever part the mouse is touching (done), while ignoring transparent parts (need help)

I am making a 2D game, and I have a script that makes the player face whichever direction the mouse is pointing, and it works properly.

while character:FindFirstChild("Humanoid").Health > 0 do
	game:GetService("RunService").Stepped:Wait()
	character.UpperTorso.CFrame = CFrame.new(character.UpperTorso.Position, Vector3.new(mouse.Hit.p.X, character.UpperTorso.Position.Y, mouse.Hit.p.Z))
end

However, I have transparent parts that act as hitboxes, and it because of this, it causes inconsistencies…

image
(In the image, the player is not facing the mouse because there is a hitbox above the tree).

How can I fix this?

You’ll probably have to swap from mouse.Hit to a raycast system, since you can use CollisionGroups with it.

  • Create 2 Collision Groups, one of them will be used for the HitBoxes.
  • Set The hitbox parts Collision Group to the “HitBoxes” collision group;
  • On the other collision group, set collision with “HitBoxes” group as false;
  • When using Raycast, set RaycastParams.CollisionGroup to the group you made that doesn’t collide with “HitBoxes” Group.

Mouse.Hit or mouse.Target will ignore parts that have the CanQuery property set to false.

But there is one thing; to change the CanQuery property, the part needs to have the CanCollide property set to false (not sure if changing it through a script will work, but you can try it by categorizing transparent parts and using a for index loop). If that doesn’t work, I suggest trying @Dfzoz’ solution, as I don’t think you can set ignorelists for the mouse, like you can with Raycasting.

I forgot to mention that these hitboxes had cancollide to false - they were meant so that players couldn’t place objects to close to another object, I may have used the wrong terminology, apologies.

I was wondering, is there something like this for meshparts too?

Yeah I think mouse also detects meshparts.