Unity – Get Targets in Range

public float           MaxAimRange = 10f;
public List<Transform> targets;
public Vector2 targetingSensorPosition;


// Start is called before the first frame update
void Start() {
  targetingSensorPosition = GetComponent<Transform>().position;
}


private void GetTargetsInRange() {
  var myContactFilter   = new ContactFilter2D();
  var collidersInCircle = new List<Collider2D>();
  var numFound          = Physics2D.OverlapCircle(
                                   targetingSensorPosition,
                                   MaxAimRange, 
                                   myContactFilter,
                                   collidersInCircle);

  Debug.Log("count of colliders: " + collidersInCircle.Count);
  Debug.Log("numFound: " + numFound);

Leave a Reply

Your email address will not be published. Required fields are marked *