3
\$\begingroup\$

I've been working on a 2D tile-based side scroller puzzle game in Unity with obstacles such as doors, keys, fans, portals, etc., but one type of obstacle, push blocks, is giving me some issues. Here is how it is supposed to work:

  • The player (an certain other obstacles) can push the block from either side, and it should move smoothly until it is either blocked by another obstacle or there is no ground beneath it
  • If it is pushed off an edge, it should fall into whatever hole is there, even if it is a snug fit. (left picture)
  • The top of the block should be flush with other blocks or surfaces when they are at the same level of height. There are no slopes in the tileset, so this would happen often. Motion across the combined surfaces should be smooth, with no way to wiggle between adjacent blocks. (right picture)
  • It must be able to respond to other forces in the game such as fans and gravity switches.

two push block scenarios

I have been using Unity's built-in physics engine, which has worked great for all other components of the game, but sometimes allows blocks to jump over holes, and sometimes blocks get stuck between other blocks when they shouldn't, so I have tried abandoning Unity's physics and made my own. It just made the rest of the game more glitchy and still failed to resolve the block problems.

If I change just the way the blocks physics work, I may be able to lock its position to a grid, where it cannot be left between tiles. That way I could get the more snug collisions to work. If I did this though, it would cause problems with the rest of the interactions. A floating block with a fan underneath it bounces around too much to be locked to a grid. Does anyone know a design that would allow all these requirements of be met?

\$\endgroup\$
  • \$\begingroup\$ Physically, when determining how an object falls, you need to look at the center of mass of the object. More than likely your center of mass is being positioned at the center of your cube. Thus, as soon as more than half of the block is over the hole, the block will start to topple over and not go smoothly down. One solution may be to move your center of mass to the edge of the block. For instance, if you are pushing your block to the right, make sure your center of mass is at the left most position. docs.unity3d.com/ScriptReference/Rigidbody-centerOfMass.html \$\endgroup\$ – Greg Petersen Mar 3 '16 at 22:12
  • \$\begingroup\$ That part isn't a problem since I disabled rotation on the block, so it already waits until 100% of the base is off the edge. The problem is that when the hole is only barely big enough the other side catches it before it has a chance to drop. \$\endgroup\$ – tyjkenn Mar 3 '16 at 22:27
  • \$\begingroup\$ Ah ok. Can you decrease the collider then slightly? \$\endgroup\$ – Greg Petersen Mar 3 '16 at 22:33
  • \$\begingroup\$ I've tried that, and it allowed the hole to catch the block sometimes but not always. I would have to shrink it quite a bit for it to work 100% of the time. And if I did shrink it, it would create more problems trying to slide things over it like in the second picture since it is no longer flush with land and there are gaps between blocks. \$\endgroup\$ – tyjkenn Mar 3 '16 at 22:45
  • \$\begingroup\$ It isn't simply too big time steps? Such that in one frame the block is on the left side of the hole, the next time step the block is on the right side of the hole? If so, consider decreasing time step or forcing it back to the hole if it passes over it. \$\endgroup\$ – Skitskraj Mar 4 '16 at 5:29
1
\$\begingroup\$

I had the same issue with my first game. I fixed the alignment issue by placing a Trigger in the hole that detects whenever a block touches it when that happens the block touching the Trigger gets destroyed and spawns a box just above the hole that would fall straight down.

\$\endgroup\$
-1
\$\begingroup\$

why not set triggers that say.. ok the block is close enough.. turn off the rigid body and lerp it over the opening then drop down my increments until its in a desired position?

\$\endgroup\$
  • \$\begingroup\$ Ideally, your answer would not be in the form of a question. Can you describe why turning off physics would be the best solution? \$\endgroup\$ – MichaelHouse May 11 '16 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.