Hello,
I upgraded to v5 and built for WebGL, and fixed the “boring loop” problem. You can play the latest version here:
http://chantage.info/blockbreaker/index.html
The approach I used for the boring loop bug, is to check the velocity in each direction, and if it’s below 1, add 1 in that direction. The random.range approach doesn’t solve the problem. And if you add velocity in each bounce the ball speeds up too fast. You can find the actual code from the ball.cs below, it’s not very elegant but it works.
void OnCollisionEnter2D (Collision2D collision) {
if (hasStarted){
GetComponent<AudioSource>().Play();
if(this.GetComponent<Rigidbody2D>().velocity.x < 1 & this.GetComponent<Rigidbody2D>().velocity.x > 0){
this.GetComponent<Rigidbody2D>().velocity += new Vector2(1f,0f);
}
if(this.GetComponent<Rigidbody2D>().velocity.x < 0 & this.GetComponent<Rigidbody2D>().velocity.x > -1){
this.GetComponent<Rigidbody2D>().velocity += new Vector2(-1f,0f);
}
if(this.GetComponent<Rigidbody2D>().velocity.y < 1 & this.GetComponent<Rigidbody2D>().velocity.y > 0){
this.GetComponent<Rigidbody2D>().velocity += new Vector2(0f,1f);
}
if(this.GetComponent<Rigidbody2D>().velocity.y < 0 & this.GetComponent<Rigidbody2D>().velocity.y > -1){
this.GetComponent<Rigidbody2D>().velocity += new Vector2(0f,-1f);
}
}
}