0
\$\begingroup\$

I have a system where physics2d objects are spawned and I need to know when they spawn outside of a region defined by a box collider set to "is Trigger".

Using onTriggerEnter and onTriggerExit to know if an object exits / re-enters the bounds (with some other conditions determining whether to kill the object when it exits the bounds). That stuff works fine.

However I can't detect when an object spawns outside of the collider (which I want to allow, but just know when it happens).

I tried the following in the Start method of the spawned object:

List<Collider2D> results = new List<Collider2D>();
_boundsCollider.OverlapCollider(new ContactFilter2D(), results);
bool spawned_outside = !results.Contains(_thisCollider);

However at the time that method runs, the booundsCollider doesn't register it. Subsequent frames it will, but that's too late.

OverlapCollder can't be run on the collider for the spawned object since it isn't set as a trigger.

Is there any way I can query the spawned object's own collider to determine if it is in contact with the bounds collider?

Thanks!

PS I can't trivially just reference the coords of the object and check if they're out of bounds because the bounds collider will move, and I want to know with precision wether just a corner is overlapping or whatever so I'd need something that amounts to my own simple collision system

\$\endgroup\$
2
  • \$\begingroup\$ Can you tell us anything about the shapes of these colliders? If you're using simple primitives like boxes/circles/capsules, we might be able to check this with pure math, without waiting for a physics tick to detect collider interactions. \$\endgroup\$
    – DMGregory
    Apr 29, 2021 at 17:15
  • \$\begingroup\$ Right now, just boxes. I'd certainly like to know that math! but I also want to allow for more complex colliders in the future \$\endgroup\$ Apr 29, 2021 at 17:17

2 Answers 2

0
\$\begingroup\$

You can use the "Awake()" behavior on the objects you want to detect spawning, and just implement the code you mentioned above:

List<Collider2D> results = new List<Collider2D>();
_boundsCollider.OverlapCollider(new ContactFilter2D(), results);
bool spawned_outside = !results.Contains(_thisCollider);

As you can see here that's called at the stage where the objects are actually initialized.

What behavior are you experiencing with calling it at "Start()" that's making it too late?

\$\endgroup\$
4
  • \$\begingroup\$ Alas the problem is the other way around. It isn't that the code I posted is running too late - it's that at the time it runs in either Awake or Start, the other collider (boundsCollider) hasn't yet detected the overlap. So "results.Contains(_thisCollider)" will return false, even though it's true, until a physics update has happened, which is what I meant by 'too late'. \$\endgroup\$ Apr 29, 2021 at 17:28
  • 1
    \$\begingroup\$ Did you try Physics2D.SyncTransforms to inform the physics engine of the position of the newly-spawned collider? \$\endgroup\$
    – DMGregory
    Apr 29, 2021 at 17:30
  • \$\begingroup\$ @DMGregory no dice :( \$\endgroup\$ Apr 29, 2021 at 18:20
  • \$\begingroup\$ @arcadeperfect Are you able to check the distance between the object that you're spawning, and the collider? Something like: if(Vector3.Distance(Collider.transform.position, gameObject.transform.position) > [colliders radius]) { // do things if the distance to collider at spawn is less then the colliders radius } \$\endgroup\$ Apr 29, 2021 at 18:38
0
\$\begingroup\$

You could just get the bounds of the collider and check whether the objects are within the bounds, then you wouldn't have to check overlap at all.

Heres an example

public BoxCollider2D box;

void Start() {
    Bounds bounds = box.bounds
    if (transform.position.x < bounds.min.x)

Obviously you wouldnt just check whether its outside of the left bounds you would want other checks too however it may work for you.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .