Reputation: 665
So I've got a prefab which I am instantiating from the Resources folder in random places; it is just an image with a SpriteRenderer attached to it at the moment:
go = (GameObject) Instantiate(Resources.Load("alienPink"));
These are instantiated at different locations after every 3 seconds.
I also have a ball with a SpriteRenderer, CircleCollider2D and a RigidBody2D attached to it and i get access to the RigidBody2D as follows:
//ray cast from camera to mouse point to detect hit
RaycastHit2D hit = Physics2D.Raycast(mousePos2D , dir);
. . .
RigidBody2D grabbedObject = hit.collider.rigidbody2D;
//do stuff with grabbedObject including change position
I want to detect when the prefab is completely overlapped by the ball which is transparent so I can start doing stuff to the prefab. I've tried numerous methods including trying to detect an overlap between the Renderer of prefab and the ball's rigidbody2D
to no avail. I've even tried grabbing all the prefabs in scene, getting them into an array and detecting overlaps by going through all of the array in every single update but it just doesn't work:
GameObject[] prefab = GameObject.FindGameObjectsWithTag("enemies"); //returns GameObject[]
Upvotes: 1
Views: 1932
Reputation: 2021
Below you will find my code for Unity to verify if object overlapping before instantiate them in the game.
Before you use below code please check if you are using correct collider in method IsOverlappingExistingGameObjects
that you setup for your object in Inspector. I'm using for example PolygonCollider2D.
var objectCollider = gameObjectToCheck.GetComponent<PolygonCollider2D>();
void SpawnGameObject(GameObject gamePrefab, float minX, float maxX, float minY, float maxY)
{
float positionX = Random.Range(minX, maxX);
float randomY = Random.Range(minY, maxY);
var randomPosition = new Vector3(positionX, randomY, 0);
var instantiateGameObject = Instantiate(gamePrefab, randomPosition, Quaternion.identity);
while (IsOverlappingExistingGameObjects(instantiateGameObject))
{
Destroy(instantiateGameObject);
// Regenerate position of new game object
positionX = Random.Range(minX, maxX);
randomY = Random.Range(minY, maxY);
randomPosition = new Vector3(positionX, randomY, 0);
instantiateGameObject = Instantiate(gamePrefab, randomPosition, Quaternion.identity);
}
}
private bool IsOverlappingExistingGameObjects(GameObject gameObjectToCheck)
{
var objectCollider = gameObjectToCheck.GetComponent<PolygonCollider2D>();
if (objectCollider != null)
{
var colliderHit = objectCollider.OverlapCollider(new ContactFilter2D(), new Collider2D[2]);
if (colliderHit > 0)
{
Debug.Log($"GameObject collider happen {colliderHit}");
return true;
}
}
return false;
}
Upvotes: 0
Reputation: 709
Try using Physics2D.OverlapCircle to detect overlaps. Another option worth checking out is: Physics2D.OverlapArea. If these two options only provides a partial solution, try using Physics2D.OverlapPoint by checking multiple points so that each point is required to be overlapped; for example, if the ball is to be completely inside a square check for the overlapping of four points within that square.
Upvotes: 1