- Home /
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;
}
Answer by aldonaletto · Oct 25, 2013 at 06:27 PM
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.
great! that was exactly the problem.
This part was in my update: mask = ~mask;
You managed to give me a good answer without my full code, your really good hehe.
Your answer
Follow this Question
Related Questions
Physics2D, weird layerMask behavior on Linecast, Raycast and OverlapPoint 2 Answers
Help with Raycast C# 0 Answers
SphereCast doesn't work with a layerMask 1 Answer
unity raycast layers in Multiplayer 0 Answers
Problem with raycast vertical 1 Answer