/// <summary> /// Picks up the closest item in range. /// </summary> protected void pickupItem() { Collider2D[] itemsInRange = Physics2D.OverlapCircleAll(transform.position, 2f, pickableItems); int nearestItem = 0; float lowestDistance = 1000f; if (itemsInRange.Length > 0) { int totalItems = itemsInRange.Length; for (int i = 0; i < totalItems; i++) { float distance = Vector2.Distance(transform.position, itemsInRange[i].transform.position); if (distance < lowestDistance) { lowestDistance = distance; nearestItem = i; } } grabbedItem = itemsInRange[nearestItem].GetComponent <PickableItem>(); if (!grabbedItem.getIsUsed() && !grabbedItem.getIsPicked()) { hasItem = true; grabbedItem.setIsUsed(false); grabbedItem.setIsPicked(true); grabbedItem.pickUp(pickUpSpot); } } }