Hi, I am creating a 2d game like the snake vs block so in that game i add the single block with sticks with it i create a spawner script that spawns it after a fix interval of time the issue is that when the spawner spawns it then it overlap the existing thing like the blocks row or power ups that i create in game so i can’t understand the logic that what i need to prevent the overlapping so i also share a code of the script spawner of singleblocksticks so tell me its solution i shall be very thankful here is the code. I am also sharing the Screen shot of the issue so that you can perfectly understand the issue.
using System.Collections;
using UnityEngine;
public class BlockWithSticksSpawner : MonoBehaviour
{
public GameObject blockWithSticksPrefab;
public Vector2 spawnAreaSize = new Vector2(2, 2); // Adjust the size as needed
public float minSpawnInterval = 3f;
public float maxSpawnInterval = 6f;
private Camera mainCamera;
private Coroutine spawnCoroutine;
private bool isSpawning = false;
void Start()
{
mainCamera = Camera.main;
StartSpawning();
}
public void StartSpawning()
{
if (!isSpawning)
{
isSpawning = true;
spawnCoroutine = StartCoroutine(SpawnBlockWithSticks());
}
}
public void StopSpawning()
{
if (isSpawning)
{
isSpawning = false;
if (spawnCoroutine != null)
{
StopCoroutine(spawnCoroutine);
spawnCoroutine = null;
}
}
}
private IEnumerator SpawnBlockWithSticks()
{
while (isSpawning)
{
// Check if we are in a subtracting state
if (!GameController.Instance.isSubtracting)
{
float spawnInterval = Random.Range(minSpawnInterval, maxSpawnInterval);
yield return new WaitForSeconds(spawnInterval);
Vector2 spawnPos = GetRandomSpawnPosition();
if (CanSpawnBlockWithSticks(spawnPos))
{
Instantiate(blockWithSticksPrefab, spawnPos, Quaternion.identity);
}
}
else
{
yield return null;
}
}
}
private Vector2 GetRandomSpawnPosition()
{
float cameraHeight = 2f * mainCamera.orthographicSize;
float cameraWidth = cameraHeight * mainCamera.aspect;
float spawnX = Random.Range(-cameraWidth / 2, cameraWidth / 2);
float spawnY = mainCamera.transform.position.y + cameraHeight / 2 + spawnAreaSize.y;
return new Vector2(spawnX, spawnY);
}
private bool CanSpawnBlockWithSticks(Vector2 spawnPosition)
{
// Perform a boxcast to check for overlapping colliders with a slight offset
Collider2D[] colliders = Physics2D.OverlapBoxAll(spawnPosition + spawnAreaSize / 2, spawnAreaSize, 0);
foreach (Collider2D collider in colliders)
{
// Ignore self
if (collider == GetComponent<Collider2D>())
{
continue;
}
// Check if collider is below the spawn position (assuming bottom row)
if (collider.transform.position.y < spawnPosition.y)
{
return false;
}
}
return true;
}
void OnDrawGizmos()
{
if (mainCamera != null)
{
float cameraHeight = 2f * mainCamera.orthographicSize;
float cameraWidth = cameraHeight * mainCamera.aspect;
Vector2 center = new Vector2(0, mainCamera.transform.position.y + cameraHeight / 2 + spawnAreaSize.y);
Gizmos.color = Color.green;
Gizmos.DrawWireCube(center, new Vector2(cameraWidth, spawnAreaSize.y));
}
}
}
This is the SS in which the singleblockstick is overlapping the blocksrow so please tell me its solution