OverLap issue

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

It looks like you don’t have a collider on the box. Check all of the objects in your scene and make sure they have colliders. Your code is checking for collider overlap with an object that does not have a collider.

I already add the colliders to all objects the issue is just when the spawner spawns the blockwithstick then it overlaps the other objects I can’t understanding how’s to resolve this issue that how to check that the object first place here if the spawner is try’s to put the other object on the same position that not to put that object on that position put it in delay or on other position.

Your screen shots show a portion of the Inspector for the blockwithstick. It does not have a collider showing. Your code is looking for overlap between two colliders. If the blockwithstick does not have a collider, there will never be overlap. If you feel that it does, show the whole Inspector.

Ohh that you are seeing in the screen shot is the empty gameobject i created for the spawner and simple i add the spawner script attached to it and the prefab of the blockwithstick add to it and I added the collider to the blockwithstick as i also provided the screen shots of that


Ah, I see. Do any of the objects have a rigid body? You need at least one of them to have a rigid body for the physics engine to register a collision.

Umm after your telling i added the rigid body 2d component to the blockwithstick perfab and do its gravity to zero but nothing changed

Now I am not sure. I will take a look at it when I get home. Probably won’t be able to reply until later this evening.

Ok

I do not have a lot of experience with 2D colliders, but if this were 3D, I would suspect that the colliders are too small, or placed in the wrong spot. I do not see anything wrong with the CanSpawnBlockWithSticks method. It seems like the colliders are not hitting each other. Check the sizes and alignments. Make sure they can overlap.

You mean that i should increase the size of the collider and placed in wrong spot mean where should i spot the collider than

Yes, make sure they are big enough and in the right spot on the blocks. If they are very small or not in the center, then they will not overlap. I just noticed that the size of the collider in the screen shot is 0.0001. This seems very small to me. Look at the collider and make sure it is big enough to cover the whole object.

Can you move this topic to the correct category!
It’s now hanging under Blender Ask
Which makes it less visible for developers.
Thank you!

agree

Privacy & Terms