I’m writing some logic to deal with spawning objects right into a recreation scene at a randomly generated location primarily based on the scale of a collider’s bounds. The logic for getting the coordinates and instantiating an object within the corresponding location is working fantastic however I’ve logic I take advantage of to be sure that the item being positioned will not be colliding with some other colliders. To detect if the gameobject I’ve positioned is colliding with something I’m utilizing Physics2D.OverlapCollider()
, passing within the collider of the gameobject. I get no colliders again although. Under is a simplified model of what I am doing:
var x = UnityEngine.Random.Vary(xmin, xmax);
var y = UnityEngine.Random.Vary(ymin, ymax);
var spawnedObjCollider = spawnedObj.GetComponent<Collider2D>();
spawnedObj.rework.place = new Vector3(x, y, 0f);
Physics2D.SyncTransforms();
var overlappingColliders = new Checklist<Collider2D>();
var numColliders = Physics2D.OverlapCollider(spawnedObjCollider, new ContactFilter2D().NoFilter(), overlappingColliders);
Within the case that I am taking a look at, the spawned gameobject is overlapping one other collider however it acts prefer it does not see it. Each objects are on the default layer despite the fact that it should not matter what layer they’re on since I’m passing in NoFilter(). (the small collider is the collider of the spawned object):
These are the collider settings, at runtime, for the spawned gameobject:
These are the collider settings, at runtime, for the collider that the spawned gameobject is being placed on prime of: