Collisions and Jerkiness Unity 2D
This must be a solved problem that I am failing to GoogleFu.
I'm learning Unity and building a simple model for a 2D puzzle game using the Unity's physics engine. Right now, I'm having a problem with collisions being/looking precise.
Case 1
When my player object, a black square, collides into the walls of the arena (four rectangles forming a square arena), the player tends to penetrate slightly before bouncing out. It's slight, and I've played with the "min penetration settings" but it's noticeable and quite annoying.
Case 2
I've been playing with the 2D Slider Joint to create a platform that can slide along an axis. When my player collides into this platform along an axis that the slider joint shouldn't move in, the joint/platform bounces.
For example, if my player is falling from top to bottom along the Y-axis, and the platform is constrained to move along the X-axis only, it'll slightly shift along the Y-axis upon collision. What's worse, it won't bounce back and will continue to play out in its shifted position. When the player object falls off (say, gravity shifts to push it from bottom to top), the platform will jump back to its original position.
Again, pretty aggravating.
Question
Are there any ways around this? Should I just be blowing up the size of my arena/player character so that these effects are less noticeable? Perhaps I'm playing with small units (My player character is 0.5 units large) so the effects are pronounced?
Or is this just something flawed with Physics systems or Unity's physics system? In this case I'll have to create my own physics system/movement system and that just seems really unfortunate for such simple cases.
Answer by conman79 · Oct 01, 2016 at 10:36 PM
Case1 is a limitation of the physics system. Physics calculations are intensive and so are limited by processing power of your system. The default physics calculation cycle in unity is 0.02 seconds, or 50 fps. Depending on the amount of physics interactions in your game and your target platform you can increase this cycle rate in Project Settings > Time > Fixed Timestep for better results. For example setting it to 0.01 will double the physics fps to 100 which will look better but also double the processing overhead. This should be fine for relatively simple physics based games.
Case2: Have you set "Freeze Position" on the Y axis in the Contstraints of your platform rigidbody? This should solve that problem. Probably should freeze Z rotation too.
The former makes sense but not at the scale I'm at. 50 fps should be more than enough to handle this jittering. The box is making it half a unit into the wall before bouncing out sometimes. I've worked with physics systems and this type of behavior is more pro$$anonymous$$ent in shooters when projectiles are moving too fast.
I set the freeze position to see if that would affect anything when debugging. It didn't. As far as I'm concerned, I shouldn't "need" to do that. Otherwise, that just invalidates the point of the 2d slider joint. Also, unfortunately, I'm going to need other axes other than X-Y so constraints wouldn't be a permanent solution even if it did work.
Is your camera moving in your game or is it static?
Currently static. The prototype is pretty much as simple as it can be.
There's a player character (black box). There's an arena (four rectangular walls, empty inside). There's a platform (rectangle inside the arena.
I apply a force to all objects inside the arena in one direction, say Y-axis down. The platform has a 2D slider joint component that prohibits it from falling down and being affected by this force. That works great.
As the player character falls, it gains speed but really isn't moving anything fast. When it strikes my platform it pushes it 1/4 a unit down before stopping.
If my character misses the platform (I delete it), it will fall onto the arena wall. About 1/5 times, it clips into it noticeably before bouncing out. Just one or two frames after I adjusted the settings, but enough that anyone would notice and think something glitched.