raycast stops working when add distance or layermask

I have the following script:

float distance = Vector3.Distance(target*.transform.position, transform.position);*
if (distance < 10)
{
int layerMask = 1 << 8;
layerMask = ~layerMask;
RaycastHit hit;
if (Physics.Raycast(transform.position, target*.transform.position, out hit, distance, layerMask))*
{ debug…
The script works perfectly until I add either “distance” (on a number instead of the word) or “layerMask” . If I remove both terms and stop the line after “out hit” it works just fine.

Make sure you read the documentation for Raycast: Unity - Scripting API: Physics.Raycast

It sounds like you’re either setting too small of a distance, or trying to set layermask an incorrect layer, or misunderstand how the layermask setting works. Also make sure the colliders are on the correct layers.

Also, 3D Physics - Unity Learn