How to keep rigid bodies objects inside a collider?
I have some balls objects that are RigidBody2D + CircleCollider2D that I want to keep inside a hourglass like sprite, that have its collider defined as an EdgeCollider2D. And this works when I start the game, the balls fall to the ground of the hourglass.
The problem is when I rotate the hourglass, I want the balls to rotate with it, but now they just keep going off the collider. I don't know it it's because the EdgeCollider2D can't be connected at both ends or if it is something else.
(I tried to use a PolygonCollider2D for the hourglass, but it seems to only keep objects OUT of the collider, not IN it, so it doesn't work for me. Unless I'm using it the wrong way).
You can see the problem more easily in this video: https://www.youtube.com/watch?v=QTwvKsd7SFg&feature=youtu.be
This is the code I use to rotate the hourglass when a button is clicked. (The rotation per se is working as expected)
public void Rotate180Degrees() {
float zRotation = transform.rotation.z;
if (isUp) {
if (zRotation > -1) {
transform.Rotate(new Vector3(0, 0, -10));
Invoke("Rotate180Degrees", 0.01f);
} else {
isUp = !isUp;
}
} else {
if (zRotation < 0) {
transform.Rotate(new Vector3(0, 0, 10));
Invoke("Rotate180Degrees", 0.01f);
} else {
isUp = !isUp;
}
}
}
Thanks in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220612085827im_/https://answers.unity.com/themes/thub/images/avi.jpg)