title: Make CircleCast stop once it hits something in Unity?
body: <p>I am working on a game where a 2D projectile can ricochet off of walls. I currently have the object itself ricocheting just fine, however I would like an <a href="https://angrybirds.fandom.com/wiki/Slingshot?file=Sling.PNG" rel="nofollow noreferrer">Angry Birds Style HUD</a> (minus the gravity) that displays where the object is going to hit a wall, and show what angle it will ricochet at: (Pardon my drawing skills)</p>
  <p><a href="https://i.stack.imgur.com/n9kyt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n9kyt.png" alt="My end goal" /></a></p>
  <p>I have this system working fine on the technical side, all except for the fact because the raycast is endless, so the actual result looks like this:</p>
  <p><a href="https://i.stack.imgur.com/XxKBh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XxKBh.png" alt="Current result" /></a></p>
  <p>So to be clear, in the end, I want the 1st raycast to go out infinitely until it hits something, and then when it generates the 2nd second raycast where the first one hit somthing, have the second raycast only go out a pre-specified distance (Maybe 3f or something like that), or until it hits something, in which case it should stop.</p>
  <p><strong>My Script:</strong></p>
  <pre><code>Transform firePoint;
  
  void Update
  {
       DrawPredictionDisplay();
  }
  
  private void DrawPredictionDisplay()
      {
          Vector2 origin = firePoint.transform.position; //unity has a built in type converter that converts vector3 to vector2 by dropping the z component
          direction = firePoint.transform.up;
          float radius = 0.4f;
          RaycastHit2D hit = Physics2D.CircleCast(origin, radius, direction);
  
          // Draw black line from firepoint to hit point 1
          Debug.DrawLine(origin, direction * 10000, UnityEngine.Color.black);
  
          if (hit)
          {          
              origin = hit.point + (hit.normal * radius);
              Vector2 secondDirection = Vector2.Reflect(direction, hit.normal);            
  
              // Create second raycast
              RaycastHit2D hit2 = Physics2D.CircleCast(origin, radius, secondDirection);
  
              if (hit2)
              {
                  if(hit.collider.gameObject.tag != &quot;Destroy&quot;)
                  {
                      // Enable prediction points
                      for (int i = 0; i &lt; numOfPoints; i++)
                      {
                          predictionPoints2[i].SetActive(true);
                      }
  
                      // Calculate reflect direction
                      Vector2 origin2 = hit2.point + (hit2.normal * radius);
                      // Draw blue line from hit point 1 to predicted reflect direction
                      Debug.DrawLine(origin, secondDirection * 10000, UnityEngine.Color.blue);
                  }      
              }
              
          }
      }
  
      Vector2 predictionPointPosition(float time)
      {
          Vector2 position = (Vector2)firePoint.position + direction.normalized * 10f * time;
          return position;
      }
  
      Vector2 predictionPointPosition2(float time, Vector2 origin, Vector2 direction)
      {
          Vector2 position = origin + direction.normalized * 10f * time;
          return position;
      }
  </code></pre>
  <p><strong>Notes:</strong></p>
  <p>While I would use regular raycasts, I found that normal Raycasts don't cut it because the raycasts are only 1 pixel wide and the object is (about) 512px by 512px, meaning the object would physically touch the wall before the raycast did, causing inaccuracy.</p>
  <p>I've already created a system that generates dots along the raycast path in a similar vein to Angry Birds so that the player can see what the raycasts are doing in the game view, but since it's not relevant to the question I removed that part of the code from my script above. Which means all I need to do limit how far the raycasts go, not find a way for the player to see what's happening. (I'm addng this in the notes to avoid bringing up the conversation of whether or not the player can see what's happening.)</p>
  <p><code>firepoint</code> is the barrel/tip of the weapon that the projectile is fired from. (The weapon rotates according to/following to the mouse)</p>