Im trying to simply get a ball to bounce with constant speed and without any infinite bounces. To avoid infinite bouncing I have added some drifting on each bounce in the collision event like this:
private void OnCollisionEnter2D(Collision2D collision)
{
Vector2 veclocityTweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
rigigidBody2D.velocity += veclocityTweak;
}
I have also rotated the colliders (the ball is bouncing within this box)
This works ok. not perfect. But my real problem is that the ball suddenly stops and the rigidBody2D.velocity sets to (0.0, 0.0) as well as the magnitude.
I tried to prevent this by this function running in the update function checking the velocity. The idea is that it should prevent the ball to go to 0 in velocity but still that happens after a while:
private void checkVelocity()
{
Vector2 vel = rigigidBody2D.velocity;
float minSpeed = 0.8f;
if (vel.magnitude < minSpeed)
{
float multipl = Random.Range(-1.0f, 1.0f);
rigigidBody2D.velocity = vel.normalized * multipl;
}
}
Here is my settings in the Project Settings:
And I have set the material for the ball to:
How can I prevent the ball from stopping?