Collider 2D Bounds before instantiation

I am making a 2D online game with PUN. The game has a procedural dungeon. In that dungeon I want to spawn Items, but to make sure I don’t spawn them on let’s say a wall I need to check if they collide with that wall.

The thing I discoverd is that you cannot get the bounds of a Collider2D component from a prefab that is not instantiated. This is a HUGE problem for me. Because it is really inefficient to first spawn the objects and in particular Photon gameobjects and then delete/move them if they hit something. So I need the bounds of the Collider2D badly!

If someone could help me, it would be life saving! Since I am on a deadline!

Thanks in advance!

You may want to create a config for object sizes that you can access pre-instantiation, either from a component on the prefab, a scriptable object association, or some other lookup table. The level of granularity is up to you, you could do per-object config, or several categories of objects with varied bounds.

The bounds are generated from the actual physics shapes because the bounds are only known then. Calculating the bounds from the collider settings would be super slow for some colliders i.e. PolygonCollider2D, EdgeCollider2D, CompositeCollider2D, TilemapCollider2D. For CircleCollider2D and BoxCollider2D it’s obviously easy.

You could easily calculate this yourself however if you are using the simple cases.

1 Like