Tuesday, April 16, 2024
HomeSample Page

Sample Page Title


I’m writing some logic to deal with spawning objects right into a recreation scene at a randomly generated location based mostly 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 ensure that the article being positioned isn’t colliding with another 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. Below is a simplified model of what I’m doing:

var x = UnityEngine.Random.Range(xmin, xmax);
var y = UnityEngine.Random.Range(ymin, ymax);
var spawnedObjCollider = spawnedObj.GetComponent<Collider2D>();
spawnedObj.remodel.place = new Vector3(x, y, 0f);
Physics2D.SyncTransforms();

var overlappingColliders = new List<Collider2D>();
var numColliders = Physics2D.OverlapCollider(spawnedObjCollider, new ContactFilter2D().NoFilter(), overlappingColliders);

In the case that I’m , the spawned gameobject is overlapping one other collider nevertheless it acts prefer it does not see it. Both 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):

enter image description here

These are the collider settings, at runtime, for the spawned gameobject:

enter image description here

These are the collider settings, at runtime, for the collider that the spawned gameobject is being placed on high of:

enter image description here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments