- Home /
Physics2D bug or something I can avoid?
Here's an animation of what happens in my game. It always reproduces in conditions shown on this gif. It's a simple breakout clone where ball destroys the bricks. You can see that it successfully hits two bricks, but when it returns back to hit that long brick, it starts to slide along it.
It looks like some kind of physics approximation bug/feature, but it's unacceptable for this kind of game because after this the ball will just keep at the same height and never reach the bottom of the screen.
Ball and all bricks have physics materials with friction=0 and bounciness=1. When it starts sliding it triggers OnCillisionEnter and only when ball stops touching the brick, it triggers Exit. Which isn't ok.
If I change velocity of the ball, it takes different trajectory and this doesn't happen, but when you play the game, sometimes this sliding bug will pop out of nowhere and make my game unplayable.
What's causing this? Is this fixable? If not, maybe there's a workaround?
Thanks.
Answer by Andrii · Jan 24, 2017 at 03:19 AM
Ok. I've found it.
It's Edit > Project Settings > Physics 2D > Velocity Threshold
Changing it from 0.1 to 0.01 fixes it for me. And from what I'm reading online, it's ok to set it to 0.0001 for this kind of game when the RigidBody is intended to never stop. This setting exists just to stop objects and stop calculating physics for them. But that's useful in a more chaotic game with more objects, not in a breakout game.
Also, I've found few other people complaining on the same issue while making a breakout game :)
Answer by Rocketman_Dan · Jan 24, 2017 at 11:25 AM
@Andrii Try making the brick collider a trigger and use OnTriggerEnter2D rather than OnCollisionEnter to detect when the ball touches it. This should prevent it from behaving like a physical collision and activate the code inside the method to execute.
This is an example of code that will make the brick disappear with no animation or embelishments. Just replace "Destroy(gameObject);" with whatever code you want to run on contact.
void OnTriggerEnter2D()
{
Destroy(gameObject);
}
Note that your ball would not naturally bounce on contact so you would have to apply some code to make that happen. Perhaps by taking the movement vector before the collision and mirroring it after collision.
Answer by Viiarge · Jan 24, 2017 at 09:33 AM
I had a similar issue and fixed it putting my OnCollisionExit2D logic into the OnCollisionStay2D method. Hope this can help.