Problem when gravity is on

Hi again, I followed Rick’s instructions and I’m quite sure I have the same build as shown in the lecture. My game works perfect when the gravity is set to zero, I wait for a while before I launch the ball and after the launch the ball bounces off the walls and the game over screen pops when the ball drops. But I noticed that when the gravity is set to a negative y value (like the default one -9,81 or any negative number) after I hit play and DO NOT launch the ball, the game over screen pops up in like 10 seconds or so. (Time changes with gravity value). I assume its something like even though the image of the ball stays above the paddle, some component of it drops with gravity and triggers the lose collider. I ll be glad if you can check it. Just hit play when there is gravity and dont launch the ball. Thanks.

Sadly I tried everything I could guess and just couldnt find the reason. The game works perfect when gravity is 0, but when there is gravity, if you dont launch the ball for 10-15 seconds the game over scene still pop ups.

Hi Ahmet,

It sounds as if this is a collision detection issue. Have you considered setting the Collision Detection mode of the Rigidbody2D component to Continuous?

Wow, thats definetely the fix! Thanks again Rob, it was bothering me for the last 3 days. There is not much information about Collision Detection in the Unity manual. It just writes “Define how collisions between Collider 2D are detected.” Sorry if its a dumb question but can you briefly explain what the difference between continuous and discrete are? Thanks for your time.

1 Like

Hi Ahmet,

Actually, the documentation does cover it quite well. Nutshell version, if you have something which is going to be fast moving, you’ll probably want to set it to Continuous. If you consider your game, not just Block Breaker, but any game you are creating, have a think about which objects are going to be moving and interacting with others.

In Block Breaker, the ball is going to be the fastest moving object, the paddle probably second, and the blocks and walls are, typically, stationary. I fully appreciate you may have levels with moving blocks, or power-ups which allow for a faster moving paddle etc.

Regarding the documentation, it’s sometimes worth reading both the 3D and 2D versions, even if you are only creating a 2D game. As an example, here’s the write-up for the Rigidybody2D.collisionDetectionMode;

The method used by the physics engine to check if two objects have collided.

I’m certain that is true, but it doesn’t give us a lot to work with. If we now take a look at the 3D equivalent
(Rigidybody.collisionDetectionMode);

The Rigidbody’s collision detection mode.

Use this to set up a Rigidbody’s for continuous collision detection, which is used to prevent fast moving objects from passing through other objects without detecting collisions. For best results, set this value to CollisionDetectionMode.ContinuousDynamic for fast moving objects, and for other objects which these need to collide with, set it to CollisionDetectionMode.Continuous. These two options have a big impact on physics performance. Alternatively, you can use CollisionDetectionMode.ContinuousSpeculative, which is generally cheaper and can also be used on kinematic objects. If you don’t have issues with collisions of fast objects, leave it set to the default value of CollisionDetectionMode.Discrete. Continuous Collision Detection is only supported for Rigidbodies with Sphere-, Capusle- or BoxColliders. See Also: CollisionDetectionMode.

So, whilst there may be a few types of collision detection there which 2D doesn’t have, it gives a much better write up.

Now, if we 've been able to see that collisionDetectionMode itself comes in two flavours, 3D and 2D, so if we look specifically for the 2D;

Properties

Discrete - When a Rigidbody2D moves, only collisions at the new position are detected.
Continuous - Ensures that all collisions are detected when a Rigidbody2D moves.

The above summaries are again a little brief but they are intended to be so, you have to dig in a little further;

When a Rigidbody2D moves, only collisions at the new position are detected.

When using this mode, Rigidbody2D that are moving fast can overlap or even pass through other colliders. This mode however is much faster to calculate and should only be used when objects are moving at relatively slow or moderate speeds and you are not encountering objects overlapping or passing through each other.

Ensures that all collisions are detected when a Rigidbody2D moves.

When using this mode, the collision detection system will detect all collisions in the path that a Rigidbody2D moves along therefore preventing colliders attached to the rigidbody passing through other colliders at higher speeds. The physics system will also calculate a time-of-time calculation to ensure that the new position of the Rigidbody2D is at the correct contact position with no overlaps. This mode however is much more expensive to calculate and should only be used when objects are moving at higher speeds or you are encountering objects overlapping or passing through each other.

Sometimes you have to go on a bit of a journey with the documentation to end up at what you need :slight_smile:

Those last two extracts above really explain it well.

As there is an overhead to using Continuous you will want to give some consideration to where you apply it, e.g. don’t just set it on everything.

Hope the above is of use :slight_smile:

Thanks a lot Rob, its perfectly clear now. From now on I ll keep in mind to check the 3d versions of subjects in Unity docs.

1 Like

You’re very welcome Ahmet.

Yeah, it is a handy technique on the more general stuff, just be aware that, like in the case above, the collisionDetectionMode had different possibilities for 3D compared to 2D, you may find this to be the case with other classes also. :slight_smile:

Privacy & Terms