Summary:
I was able to achieve a perfect bouncing ball using an initial velocity of 0, but when I give it a non-zero initial velocity or apply any force, the ball keeps gaining height.
Baseline:
-
Create a ball with a Rigidbody2D with no Auto Mass, Mass 1, Linear Drag 0, Angular Drag 0.05, Gravity Scale 1, not Kinematic, no Interpolate, Start Awake, Continuous Collision Detection, no constraints.
-
Add a Circle Collider 2D to the ball with a material with 1 bounciness, 0 friction, not trigger, no effector.
-
Create a floor with a Box Collider 2D with default material, is trigger, no effector.
-
Attach a script to the ball so that when you press spacebar it sets it’s transform.position to be somewhere above the floor and it’s GetComponent().velocity = new Vector2(0f, 0f);
-
The ball will bounce perfectly to its original height each time.
How to break it:
- Change the value in bold above to 1f.
Expected behavior:
- I would expect the ball to go up a little at first and then keep bouncing to that new height over and over again.
Observed behavior:
- The balls does go up a little at first, but then it goes up higher and higher and higher with each bounce.
Things I tried:
-
Setting velocity to 0f, but then using AddForce(new Vector2(0f, 1f),ForceMode2D.Impulse) to cause the upward motion. – This produces the same deviation from expected behavior as above.
-
Setting the velocity (or adding force) inside a FixedUpdate() call instead of an Update() call. – This produces the same deviation from expected behavior as above.