How do I get a 2D RayCast to completely ignore colliders with a certain tag Unity 2D

Viewed 518

The raycast will shoot out of the gun and hit the gun itself. If it doesn't hit itself, it hits the player holding the gun. The player is a ragdoll and often flops in front of the gun. I want it to shoot out and ignore itself and the player and only detect the scenery. I am also confused as how to use Debug.DrawRay(Position, direction, color); How do I add length to it? Does debug.drawray not have a length option? I've been struggling with this for a couple days now (I am a beginner) any help is greatly appreciated, thank you so much!

         if (Input.GetKey(KeyCode.S))
         {
             if (Input.GetKeyDown(KeyCode.D))
             {
                //run shoot function

                 StartCoroutine(Shoot());

             }
         }
     }
     IEnumerator Shoot()
     {

         //direction = destination - source
         Vector2 direction = (ShootHere.transform.position - transform.position);
         RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, direction, 40);
         Debug.DrawRay(transform.position, direction, Color.red);
       //This is me trying to get the raycast to not hit the player hitbox, this does not do that, it just causes 
       //the raycast to end the detection when it hits the object and instead procead to draw a line (The else statement)
       //How do I get my raycast to ignore the tagged objects and to detect the next object past itself and the player?
         if (hitInfo && hitInfo.collider.tag != "Player1" && hitInfo.collider.name != gameObject.name)
         {
             Debug.Log("RayCast hit a hitbox, the name is" + hitInfo.collider.name);
             lineRenderer.positionCount = 2;
             lineRenderer.SetPosition(0, transform.position);
             lineRenderer.SetPosition(1, hitInfo.point);
         }
         else
         {
             lineRenderer.positionCount = 2;
             lineRenderer.SetPosition(0, transform.position);
             lineRenderer.SetPosition(1, ShootHere.position + ShootHere.right * 100);
         }
         lineRenderer.enabled = true;
         yield return new WaitForSeconds(0.02f);
         lineRenderer.enabled = false;
     }
 }

}

1 Answers
Related