2
\$\begingroup\$

I have a Rigidbody2D with a CircleCollider2D, on which I display a sprite of a circle. This Rigidbody can be "shot" in my game.

I also have 4 rectangular BoxCollider2Ds on the edges of the game screen.

When I shoot the circle, it rebounds fine from the rectangular colliders, except for cases when the impact velocity normal to the rectangular collider's surface is very small. In that case, the rebound velocity in that direction is somehow zero.

Here is the circle's RigidBody2D and CircleCollider2D setup:

enter image description here

Here is the BoxCollider2D of the rectangular collider:

enter image description here

The material "New Physics Material 2D" attached to the ball and the BoxColliders has Friction set to 0 and bounciness set to 1:

enter image description here

Here are two gifs, the first one demonstrating how it works (correctly) in most cases, and the second one demonstrating what happens on a low velocity impact. The "choppiness" of these gifs is due their "giffy" nature. In my game, the movement is smooth.

enter image description here

enter image description here

I'm wondering why, on the low-velocity impacts, the ball doesn't rebound in the same way as on the high-velocity impacts (i.e, by the outbound angle = inbound angle rule)?

----------------------------

Edit:

----------------------------

I stepped into the circle's void OnCollisionEnter2D(Collision2D collision) method, and, sure enough, on the very slow collisions, the collision.otherRigidbody.velocity had a zero-component for the slow velocity of the incoming collision (so depending on whether the impact's x- or y-velocity was slow, the respective component of the resulting velocity was, for whatever reason, set to 0).

Oddly enough (at least for me - I am still not completely familiar with all of Unity's in's and out's), the collision.relativeVelocity had been updated to hold the correct resulting velocity of the impact. Thus, by adding following code to the OnCollisionEnter2D method, I am able to handle the collision correctly, even on low-impact collisions:

if (collision.otherRigidbody.velocity.x == 0)
{
    collision.otherRigidbody.velocity = new Vector2(collision.relativeVelocity.x, collision.otherRigidbody.velocity.y);
}
if (collision.otherRigidbody.velocity.y == 0)
{
   collision.otherRigidbody.velocity = new Vector2(collision.otherRigidbody.velocity.x, collision.relativeVelocity.y);
}

While this seems to solve the issue I am experiencing, it doesn't seem to me that I should need to be doing this...

\$\endgroup\$
6
  • \$\begingroup\$ You say the bouncy physics material is used on your ball and box colliders, but in your gif the box colliders show no physics material applied. Was this captured from an older version, or is it possible the material didn't get applied as expected? \$\endgroup\$ – DMGregory Jan 20 '20 at 17:11
  • \$\begingroup\$ Hey DMGregory, thanks for your comment. You are right, that was captured from an old version. I have updated the post with a new gif. \$\endgroup\$ – Chris Jan 20 '20 at 19:50
  • 1
    \$\begingroup\$ Could some of the energy of the collision be going into adding spin to the ball, or something like that? \$\endgroup\$ – Foxwarrior Jan 20 '20 at 20:13
  • \$\begingroup\$ I am not sure... I did try setting the angular drag on the circle's RigidBody2D to 0.00 (it was set to 0.05 by default, as can be seen in the first screenshot above), but that did not change the behaviour.. I also played around with the Collision detection mode (Discrete versus Continuous) and the Interpolation Mode (None, Interpolate, Extrapolate) of the circle's RigidBody2D, but that didn't change the behaviour either :/ \$\endgroup\$ – Chris Jan 20 '20 at 23:25
  • \$\begingroup\$ I have added an edit at the end of the original post, as I seem to have found a way around this issue. But it doesn't seem to me that I should need to be doing what I am describing in that edit... \$\endgroup\$ – Chris Jan 21 '20 at 0:12
2
\$\begingroup\$

I had the same issue days ago while creating a pong game. The problem was with the Velocity Threshold of the Physics Engine provided by unity. You need to tune it to get a smooth bounciness effect. This is the overall set up (i.e. 1 and 2 is already there in your project)

  1. Angular Drag and Drag of the rigidbody2d = 0
  2. Physics2d Material's bounciness = 1
  3. Finally, Go to Edit -> Project Settings -> Physics2D and change Velocity Threshold to something lower (I used 0.03)
\$\endgroup\$
1
  • \$\begingroup\$ Perfect, that fixed it, thanks! :) \$\endgroup\$ – Chris Jan 23 '20 at 5:51

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.