Hi,
I have two solutions or rather hacks to the OP’s problem. Both of them seemed to work for the entire duration of my game (5 levels) when playing myself or when enabling auto-play (with a bit of trial and error while tweaking the add on velocities).
Hack 1:
I think the solution offered by @Hagyto is very good (Thanks for Sharing
) and I also had thought among the same lines and had come up with this piece of code:
if (gamestart && col.gameObject.GetComponent<Paddle>())
{
if(this.transform.position.x <= pddl.transform.position.x -1f)
{
this.rigidbody2D.velocity += new Vector2 (Random.Range(-0.2f,0f),Random.Range(-0.2f,0f));
}
if(this.transform.position.x > pddl.transform.position.x - 1f && this.transform.position.x < pddl.transform.position.x + 1f)
{
this.rigidbody2D.velocity += new Vector2 (0f,Random.Range(0f,0.2f));
}
if(this.transform.position.x >= pddl.transform.position.x + 1f && this.transform.position.x < pddl.transform.position.x + 8f)
{
this.rigidbody2D.velocity += new Vector2 (Random.Range(0.2f,0),Random.Range(-0.2f,0f));
}
}
(I had set my paddle width to the width of my playspace to test this code i.e. 16f)
Basically what I did here was add the tweaked velocity according to where the ball hit the paddle i.e if the ball hit the left side (0f to 7f) , I added only negative x velocity; when it hit center (7f to 9f) , no x velocity was added; and similarly when the ball hit the right side of the paddle (9f to 16f) , I added positive x velocity (I have kept this project on the back burner for a while and I foolishly forgot to write down the optimum tweak velocity I came up with. Hence, if you apply this, you might have to do a bit of trial and error. Sorry for that…
).
Anyway, this hack worked but offered somewhat (not perfect) of the smooth game-play you see in Arkanoid kind of games. I think Hagyto’s solution is a more effective and simplified version of this hack but I still thought this was worth mentioning and it might help someone out. ![:smiley: :smiley:](https://sjc3.discourse-cdn.com/business5/images/emoji/twitter/smiley.png?v=5)
Hack 2:
Here is the code:
if(rigidbody2D.velocity.x >= -0.5f && rigidbody2D.velocity.x <= 0.5f)
{
if(pddl.transform.position.x <= 8f)
{
Vector2 tweak = new Vector2(2f, Random.Range(-0.2f,0.2f));
}
if(pddl.transform.position.x > 8f)
{
Vector2 tweak = new Vector2(-2f, Random.Range(-0.2f,0.2f));
}
}
else if(rigidbody2D.velocity.y >= -0.5f && rigidbody2D.velocity.y <= 0.5f)
{
if (this.transform.position.y <= 9f)
{
Vector2 tweak = new Vector2(Random.Range(-0.2f,0.2f), 2f);
}
if (this.transform.position.y > 9f)
{
Vector2 tweak = new Vector2(Random.Range(-0.2f,0.2f), -2f);
}
}
else
{
Vector2 tweak = new Vector2(Random.Range(-0.2f,0.2f), Random.Range(-0.2f,0.2f));
}
In this code, I checked the velocity of the ball. If it came anywhere near a horizontal or vertical boring loop (-0.5f to 0.5f) , I added 10 times the velocity that I was adding initially for the next collision hence taking it out of the boring loop before it even enters it.
The direction the tweak was added depended on the position of the ball as it collided with the paddle (vertical boring loop) or the walls (horizontal boring loop).
Due to this, the behavior of the ball may seem a bit odd at the point the fix is applied , but it was certainly effective.
This particular fix can be rid of that odd behavior if we apply the tweak according to the direction the ball was moving when it hit the collider (paddle or wall), but I haven’t yet done that (on my TODO list).
I certainly hope this helps and maybe leads to an optimum solution. If there is any way to simplify my code or logic, please let me know.
Thanks & Cheers,
YA