- Home /
RaycastHit2D hit all layermask in the scene
public LayerMask mask;
void Start()
{
//StartCoroutine(YY());
mask = LayerMask.GetMask("chara");
}
void Update()
{
RaycastHit2D hit = Physics2D.CircleCast(transform.position, 1f, Vector2.zero, mask);
if (hit.transform != null)
Debug.Log(hit.transform.name);
}
The GameObject hit all gameobject with Collider2D component in the scene, Am i wrong?
Answer by pmurph03 · Jul 21, 2020 at 08:14 PM
Without knowing the names of your layers, is the name of the layer you want to cast against named "chara"
Perhaps try setting the mask in the inspector instead of in start()
Yes, I am already did it for the same result. I am confused it compare with this code and get different result.
hit = Physics2D.Raycast(transform.position, go.position - transform.position,4f, mask);
if (hit.transform != null)
Debug.Log("RaycastAll: "+hit.transform.name);
hits = Physics2D.CircleCast(transform.position, 1f, Vector2.zero, mask);
if (hits.transform != null)
Debug.Log("CircleCastAll: "+hits.transform.name);
Raycast is work! But CircleCast also hit all gameobject
Your answer
Follow this Question
Related Questions
Is there a convenient way to check if two gameobjects are touching BUT NOT overlapping? 2 Answers
RaycastHit2D How to return angle from one side? 2 Answers
Make mask layer have priority over other mask layer on raycast 1 Answer
Physics2D.CircleCast produces no result whenever a layer mask is specified 1 Answer