if (target.Length > 0)
{
foreach (var item in target)
{
wantedPos = Camera.main.WorldToScreenPoint(item.position);
if (wantedPos.z > 0 && wantedPos.x < Screen.width / 2 + circleRect.rect.width / 2 && wantedPos.x > Screen.width / 2 - circleRect.rect.width / 2
&& wantedPos.y < Screen.height / 2 + circleRect.rect.height / 2 && wantedPos.y > Screen.height / 2 - circleRect.rect.height / 2)
{
wantedPos.z = 0;
wantedPos.y = Mathf.Clamp(wantedPos.y, Screen.height / 2 - circleRect.rect.height / 2, Screen.height / 2 + circleRect.rect.height / 2);
wantedPos.x = Mathf.Clamp(wantedPos.x, Screen.width / 2 - circleRect.rect.width / 2, Screen.width / 2 + circleRect.rect.width / 2);
transform.position = wantedPos;
}
else
{
transform.position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
}
}
}
when there are several targets this code works for only one of them. what is the point that I’m missing here?