- Home /
[2D] Tetris game with rigidbodies snapping bug
Hi,
I'm making a quick tetris clone; i made the blocks fall by giving them a rigid body and forcing to constraint on the x axis but whatever I do; i still end up having edge snapping and other weird actions.
float number = transform.position.x;
float _x = (float)Mathf.Round(number *) / 2;
transform.position = new Vector3 (_x, transform.position.y);
Even tried to 'snap' the x position to the nearest half value but i still have problems.
I'm afraid that if i use kinematic; i won't be able to handle collisions (with other blocks or ground)
Any ideas here ? (I don't want the blocks to move 1 unit by 1 unit, but pixel by pixel instead)
Thansk
Another and (maybe cleaner way to do this), is not to rely on collisions detections with rigidbodies, but creating a workaround using raycasts.
When instantiated or move, the block casts a ray downwards. According to the y position of the impact point, you know your block must not go below this limit.
Thanks for the reply; i've thought of this alternative too; but I imagine having, i don't know, a hundred of blocks raycasting every frame can be very costly especially on mobile; it's ok for a platformer since you have only a few objects ...
$$anonymous$$mmh any other idea ?
I have never said you had to raycast every frame, only when the block is instantiated or when moving horizontally
I think making a tetris game by physic collision detection is not a good idea . I have made several tetris games in unity . Actually every game studio that I've worked at asked me to build a tetris game for them .
So , I highly recommend you to avoid physics because you have no control for your blocks as you have already encounter one of them.Ins$$anonymous$$d create a 2d array of cells that your block can move through them . This way you have more control over your blocks . Every frame you can check if your blocks can move down further by checking next down cell to see if it occupied by other block or not ? If it's free your blocks can continue moving down . Also you can smooth your blocks movement , They have not to move 1 by 1 unit . lerp the position of them until they reach other cell .
I hope it can help you.
Your answer
Follow this Question
Related Questions
How do I Spawn an Object on a Seafloor Randomly? 0 Answers
Detect Contact between Kinematic and Non-kinematic bodies 1 Answer
2D physics, animation-controlled kinematic colliders not affecting momentum 1 Answer
How to check for collision between a moving kinematic object and an empty object? 3 Answers
The character velocity changes while the rigidbody is kinematic 1 Answer