- Home /
Rotating a PolygonCollider2D does not change vertex points,PolygonCollider2D points not changing when rotating
Hello.
I'm new to unity and am trying to make the 2D game tetris as an exercise to learn how to use the engine.
Since the shapes are not exactly rectangular boxes, I decided to use the polygon collider for detecting collisions with other objects.
I read the documentation on PolygonCollider2D located here, and decided that using the "points" attribute of the collider with the bounds attribute of an empty object called GameArea that has the exact dimensions of the board the would work for me.
I set the points for each shape so that they wrap around it perfectly, then wrote the following script:
public class Tetromino : MonoBehaviour
{
private PolygonCollider2D myCollider;
private BoxCollider2D gameAreaCollider;
void Start()
{
myCollider = GetComponent<PolygonCollider2D>();
gameAreaCollider = GameObject.Find("GameArea").GetComponent<BoxCollider2D>();
}
void Update ()
{
if(!isInBounds())
{
Debug.LogError("OUT OF BOUNDS");
}
if(Input.GetKeyDown("q"))
{
rotateClockwise();
}
}
void rotateClockwise()
{
transform.Rotate(0, 0, -90);
}
bool isInBounds()
{
myCollider = GetComponent<PolygonCollider2D>();
Vector2 position = transform.position;
foreach (Vector2 point in myCollider.points)
{
if(!gameAreaCollider.bounds.Contains(point + position))
{
return false;
}
}
return true;
}
}
All of the above code works visually as expected (the shape rotates successfully) however, the "points" don't change, so when part the piece is out of bounds, nothing happens.
The math needed to manually rotate the points is simple enough to do, but I want to know if there's a way to do that without having to make that calculation every time.
Thank you very much in advance!
Answer by Bdiebeak · May 12, 2020 at 05:04 PM
Hello!
I know that it is too late... But maybe it will help somebody else.
I think that you should use TranformPoint() for transform points of collider to world space.