- Home /
 
               Question by 
               freshBakedPie314 · Jun 14, 2021 at 10:53 AM · 
                raycasthit2dray cast  
              
 
              Ignore raycasts hiting player
I want the rayCast to register all hits except the ones on the player The player is on Player layer. I used a layerMask and selected all layers except the player but that dint work heres the code im using:
 IEnumerator Shoot()
     {
         RaycastHit2D hit = Physics2D.Raycast(shootOrigin.transform.position , shootOrigin.transform.right , layerMask);
         if(hit)
         {
                 Debug.Log(hit.transform.name);
                 Enemy enemy = hit.transform.GetComponent<Enemy>();
                 if (enemy != null)
                 {
                     enemy.TakeDamage(damage);
                 }
 
                 lineRenderer.SetPosition(0, shootOrigin.transform.position);
                 lineRenderer.SetPosition(1, hit.point);
 
         }
         else
         {
             lineRenderer.SetPosition(0, shootOrigin.transform.position);
             lineRenderer.SetPosition(1, shootOrigin.transform.position + shootOrigin.transform.right * 100);
         }
         lineRenderer.enabled = true;
         yield return new WaitForSeconds(0.02f);
         lineRenderer.enabled = false;
     }
 
               Comment
              
 
               
              Answer by Patrickmol · Jun 14, 2021 at 11:31 AM
Let the player on the default layer, put the enemy on a new layer "Enemy", put layerMask value as "Enemy" layer... just that
Your answer
 
 
             Follow this Question
Related Questions
ScreenPointToRay question 1 Answer
RaycastHit2D hits "itself" 3 Answers
Make mask layer have priority over other mask layer on raycast 1 Answer
CircleCast hits emitter with Collider2D 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                