I have an object in my game which has bounciness 1 and a type of surface in my game which has bounciness 0. An essential gameplay mechanic is that this bouncy object will bounce off of everything, except the surface, which it will glide along without bouncing.
As far as I can tell, this is impossible to do in Box2D, as seen here.
It forces the maximum restitution. I do not know how I can edit this line of code inside Unity, and it is not possible to port to 3D physics at this point.
Searching unity forums suggests that I do something with OnCollisionEnter2D, for example something like this:
public class ProjectOnHit: MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D other)
{
Rigidbody2D r = other.gameObject.GetComponent<Rigidbody2D>();
r.velocity -= other.contacts[0].normal * Vector2.Dot(r.velocity, other.contacts[0].normal);
}
}
The problem with that is that the physics system appears to simulate the rigidbody upto the collision somewhere in the middle of the timestep, simulate the rigidbody after the collision (meaning a bounce), then call OnCollision after the timestep is finished. The result is that some fraction of a timestep receives a rebound, so instead of not bouncing it results in a handful of tiny bounces. This causes certain level setups to not work since the ball is meant to slide into narrow corridors and often has high velocity.
If the Box2D source was exposed it would literally be one keystroke. Is there some easy solution I am not thinking of here?