private void SpawnCluesAndBomb()
{
Vector2 firstCluePos;
Vector2 secondCluePos;
Vector2 thirdCluePos;
Vector2 bombPos;
bool isGood = false;
firstCluePos = new Vector2(RandomInt(0, 256), RandomInt(0, 256));
do
{
secondCluePos = new Vector2(RandomInt(0, 256), RandomInt(0, 256));
} while (Vector2.Distance(firstCluePos, secondCluePos) < 200f);
do
{
thirdCluePos = new Vector2(RandomInt(0, 256), RandomInt(0, 256));
} while (Vector2.Distance(firstCluePos, thirdCluePos) < 200f && Vector2.Distance(secondCluePos, thirdCluePos) < 200f);
do
{
bombPos = new Vector2(RandomInt(0, 256), RandomInt(0, 256));
} while (Vector2.Distance(firstCluePos, bombPos) < 300f && Vector2.Distance(secondCluePos, bombPos) < 300f && Vector2.Distance(thirdCluePos, bombPos) < 300f);
Instantiate(_clue, firstCluePos, Quaternion.identity);
Instantiate(_clue, secondCluePos, Quaternion.identity);
Instantiate(_clue, thirdCluePos, Quaternion.identity);
Instantiate(_bomb, bombPos, Quaternion.identity);
Debug.Log($"Distance 1: {Vector2.Distance(firstCluePos, bombPos)}, Distance 2: {Vector2.Distance(secondCluePos, bombPos)} Distance 3: {Vector2.Distance(thirdCluePos, bombPos)}");
}
using UnityEngine;
public class ObjectSpawner : MonoBehaviour
{
public GameObject firstObjectPrefab;
public GameObject secondObjectPrefab;
public GameObject thirdObjectPrefab;
public GameObject bombObjectPrefab;
private Vector2 firstPos;
private Vector2 secondPos;
private Vector2 thirdPos;
private Vector2 bombPos;
private void Start()
{
SpawnObjects();
}
private void SpawnObjects()
{
bool validPositions = false;
while (!validPositions)
{
// Generate random positions for all objects
float angle = Random.Range(0f, Mathf.PI * 2f);
float distanceFromCenter = Random.Range(0f, 200f);
firstPos = new Vector2(Mathf.Cos(angle) * distanceFromCenter, Mathf.Sin(angle) * distanceFromCenter);
angle = Random.Range(0f, Mathf.PI * 2f);
distanceFromCenter = Random.Range(0f, 200f);
secondPos = new Vector2(Mathf.Cos(angle) * distanceFromCenter, Mathf.Sin(angle) * distanceFromCenter);
angle = Random.Range(0f, Mathf.PI * 2f);
distanceFromCenter = Random.Range(0f, 200f);
thirdPos = new Vector2(Mathf.Cos(angle) * distanceFromCenter, Mathf.Sin(angle) * distanceFromCenter);
angle = Random.Range(0f, Mathf.PI * 2f);
distanceFromCenter = Random.Range(0f, 256f);
bombPos = new Vector2(Mathf.Cos(angle) * distanceFromCenter, Mathf.Sin(angle) * distanceFromCenter);
// Check if all positions are valid
if (Vector2.Distance(firstPos, secondPos) >= 200f &&
Vector2.Distance(secondPos, thirdPos) >= 200f &&
Vector2.Distance(thirdPos, firstPos) >= 200f &&
Vector2.Distance(firstPos, bombPos) >= 256f &&
Vector2.Distance(secondPos, bombPos) >= 256f &&
Vector2.Distance(thirdPos, bombPos) >= 256f)
{
validPositions = true;
}
}
// Spawn objects at the generated positions
Instantiate(firstObjectPrefab, firstPos, Quaternion.identity);
Instantiate(secondObjectPrefab, secondPos, Quaternion.identity);
Instantiate(thirdObjectPrefab, thirdPos, Quaternion.identity);
Instantiate(bombObjectPrefab, bombPos, Quaternion.identity);
}
}