Physics.Linecast seems to be hitting itself

Hello,

I’m making a radar that is showing if two elements are visible from each other, and making a material change if so or not. I’m using a LayerMask on the object.

It works, but the raycast seems to be hitting on itself and makes the if statement shuffle quickly between true and false when the result is false (making the material flash)

Here are the console/debug lines that makes me think the raycast his hitting on itself.

First one is what makes me think the raycast his hitting itself (wich is probably not the case but I can’t seem to find an answer to this line):

Blocked by UnityEngine.RaycastHit
UnityEngine.Debug:Log(Object)
radarScript:Update() (at Assets/script/radarScript.cs:42)

Seccond debug line shows up when the result from the if is false.(wich is correct)

Visible between 1 and Main Camera
UnityEngine.Debug:Log(Object)
radarScript:Update() (at Assets/script/radarScript.cs:46)

And my code:

public Transform shooter;
public Transform runner1;
RaycastHit hit;	
public LayerMask mask;
public Material visibleMat;
public Material invisibleMat;
public GameObject visibleMarker;	


mask = ~mask;
		
	 	if (Physics.Linecast(shooter.position, runner1.position,out hit, mask)) {
        	Debug.Log("Blocked by "+hit);
			visibleMarker.renderer.material = invisibleMat;	
			
  		}else{
        	Debug.Log("Visible between " + runner1.name + " and " + shooter.name);
			visibleMarker.renderer.material = visibleMat;				
  	  	}

This code is incomplete, but apparently on line 10 you’re toggling the mask, inverting all bits before doing the Linecast: this way one time you will use the correct mask, and the next time you’ll use exactly its opposite, producing the flashing effect.