boxCollider2D don't move with a tile object
I have a MapTile prefab, which is a gameObject with a SpriteRenderer and a BoxCollider2D.
BoxCollider2D fills the tile entirely. It's size is 1x1 in terms of game units.
Using this prefab I create a map, changing sprites and layers for walls (should be colliding) and floors (should not be colliding). Also changing position by transform.position = new Vector3(x, y). I do this step in SceneManager.sceneLoaded event.
After that, in the same event I create some nodes (node is just and object with coordinates and worldPosition, it's not a gameObject) and check connections between them by casting a fixed size box in 8 directions (4 orthogonal 1 unit distance, 4 diagonal ~0.75 distance as the nodes are in a diamond shaped net). No matter where I put the node, it detects any collision only in one spot on the scene, exactly where the MapTile prefab would lie if it's position wouldn't change.
When I start the game and move the player around, collisions for every wall work fine, they are detected in the right places, where the tiles lie. I check them in the FixedUpdate method.
I read about a rigidbody for moving colliders but 1. I need to move these tiles only once, while loading the scene, after that they are completely static 2. I added a Rigidbody2D component, set it to static and the effect was exactly the same. Nodes in SceneManager.sceneLoaded event do not collide, player in fixedUpdate collide.
My guess is that collider doesn't update it's position until the FixedUpdate is called, but I'd like to test it during the scene loading process.
Please advice as to how to move collider during the onSceneLoaded event with it's gameobject being moved.
Also, before this I used a tilemap, which was manually created on the grid, with the same collision principles and nodes worked exactly as expected then, so the problem must lie strictly in moving gameObjects with BoxCollider2D on them.
Answer by Gallareton · 5 days ago
There is method Physics2D.SyncTransforms() which does exactly what I needed - triggers colliders position recalculation.