Any feedback is appreciated.
public Vector2 mousePosition;
private Defender[] takenPositions;
private void OnMouseDown()
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosition.x = Mathf.Round(mousePosition.x);
mousePosition.y = Mathf.Round(mousePosition.y);
if (IsPositionOpen(mousePosition) && Button.selectedDefender)
{
Instantiate(Button.selectedDefender, mousePosition, Quaternion.identity);
}
}
private bool IsPositionOpen(Vector2 mousePosition)
{
takenPositions = GameObject.FindObjectsOfType<Defender>();
foreach (Defender component in takenPositions)
{ Vector2 convert2dPosition = new Vector2(component.transform.position.x, component.transform.position.y);
if (convert2dPosition == mousePosition) {
return false;
}
}
return true;
}