Hello,
This solution worked for me, I’m not a coding genius but I wanted to find a way of making it so my game wouldn’t have any boring game loops without using Random.Range(); (to make the user have more game control).
So what I did was look around for some solutions, I finally found a good solution for the majority of looping situations, which was to change the paddles box collider to a polygon collider, then editing that collider so that the top left and right corners was dragged down by around 1/5th of the paddles size, making the user have more control over where the ball is going as well as stopping the ball from going in a loop.
However there was still a problem that I found happening, which was that the ball could, if unluckily positioned correctly, would stay right at the edge of the left or right sides of the screen. Even with this new polygon collider method, it still failed to push it out of this loop, so I came up with a basic solution:
Inside the balls update function:
if(gameStarted)
{
if(this.transform.position.x >= 15.85f)
{
this.transform.position = new Vector2(15.80f, this.transform.position.y);
} else if (this.transform.position.x <= 0.15f) {
this.transform.position = new Vector2(0.20f, this.transform.position.y);
}
}
This makes it so that if the ball was to ever get too close to the edge, it would slightly bump it in the opposite direction, then allowing the paddle with the polygon collider to start pushing it again on the balls next bounce. From a few tests this amount of a budge is working fine and doesn’t make it look weird, as the ball is only moving a small distance it just looks as if the it slightly bounced of the wall.
Edit: If anyone has a better solution than this, please post it in the comments below.
I hope this helps anyone and everyone with this problem, however the Random.Range that Ben uses is still a good idea, but in my opinion I would prefer to use it as a mini-game mode for Block Breaker, that would be used to make the game harder as the player would not know the exact direction the ball would go in, in this game mode I would use a box collider again and make it so that when bouncing from the paddle it can go in a random direction (left or right).
– Recable –