LayerMask.NameToLayer returns the index of the layer as seen in the layer inspector, not the mask.
Pretty easy mistake to make, since it’s the LayerMask class, so I hope Unity would update their comment for the NameToLayer method to make this clearer.
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask)
Both failed for the correct layerMask. and if I do it like @foxor , it works… But the result is not what I want since other objects of different layers will also return true if a ray intersection happens.
If I change to Collider.Raycast, there’s no such problem.
Had similar problem, does your target object is child of a game object that have a Rigidbody and a collider?
try adding a kinematic rb. to your target.