I'm practicing Unity by developing a 2D game where the player has to stack tetrominoes falling from the sky. The problem I've ran into is that upon a 90 degree rotation of the falling piece, the piece sometimes overlaps with the existing tower causing two pieces to be stuck together.
I would like to check if this overlap will happen before rotating my piece.
I'm aware that such a problem would probably be solved by rotating a copy "shadow" object and checking for collision and yet I have found no way to check if an object is colliding directly after spawning it. I'm also aware of the Physics2D.OverlapBox method but I'm really looking for a way to predict collision with a more complicated 2D shape.
Current code for the shadow collider attempt:
GameObject shadow = new GameObject(); shadow.transform.position = currentTetrisPiece.transform.position; shadow.transform.rotation = currentTetrisPiece.transform.rotation; PolygonCollider2D shadowCollider = shadow.AddComponent<PolygonCollider2D>(); PolygonCollider2D currentCollider = currentTetrisPiece.GetComponent<PolygonCollider2D>(); shadowCollider.isTrigger = true; shadowCollider.points = currentCollider.points; shadow.transform.RotateAround(rotationPoint, new Vector3(0, 0, 1), 90); Physics2D.SyncTransforms(); bool collided = shadowCollider.IsTouchingLayers(); if (!collided) { currentTetrisPiece.transform.RotateAround(rotationPoint, new Vector3(0, 0, 1), 90); }